<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="ElasticSearch详解, 博客 blog">
    <meta name="description" content="熊猫小二的博客  xmxe&#39;s blog">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- 为了引用qq空间图床文件 -->
    <meta name="referrer" content="no-referrer">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>ElasticSearch详解 | 熊猫小二</title>
    <link rel="icon" type="image/x-icon, image/vnd.microsoft.icon" href="/blog/favicon.ico">
    <link rel="stylesheet" type="text/css" href="/blog/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/my.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/loading.css">

    <script src="/blog/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 6.3.0"></head>



   
<style>
    body{
       background-image: url(/blog/medias/cover.jpg);
       background-repeat:no-repeat;
       background-size:cover;
       background-attachment:fixed;
    }
</style>



<body>
    
  <div id="loading-box">
    <div class="loading-left-bg"></div>
    <div class="loading-right-bg"></div>
    <div class="spinner-box">
      <div class="configure-border-1">
        <div class="configure-core"></div>
      </div>
      <div class="configure-border-2">
        <div class="configure-core"></div>
      </div>
      <div class="loading-word">加载中...</div>
    </div>
  </div>
  <!-- 页面加载动画 -->
  <script>
    $(document).ready(function () {
      // document.body.style.overflow = 'auto';
      document.getElementById('loading-box').classList.add("loaded")
    })
  </script>

    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/blog/" class="waves-effect waves-light">
                    
                        <img src="/blog/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">熊猫小二</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="#" class="waves-effect waves-light">

      
      <i class="fas fa-book" style="zoom: 0.6;"></i>
      
      <span>归档</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/blog/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>归档</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/tags">
          
          <i class="fas fa-tags" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>标签</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>分类</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友链</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="#" class="waves-effect waves-light">

      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/blog/about">
          
          <i class="fas fa-star-of-david" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>主页</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/gallery">
          
          <i class="fas fa-images" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>相册</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
  
    <li>
      <a class="waves-effect waves-light" onclick="switchNightMode()">
        <i id="sum-moon-icon" class="fas fa-sun" style="zoom:0.65;" title="切换主题"></i>
      </a>
    </li>
  
  
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
          <img src="/blog/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">熊猫小二</div>
        <div class="logo-desc">
            
            熊猫小二的博客  xmxe&#39;s blog
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/blog/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-book"></i>
			
			归档
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  >
              
                <li>

                  <a href="/blog/archives " style="margin-left:75px">
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:50px" ></i>
			      
		          <span>归档</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/tags " style="margin-left:75px">
				  
				   <i class="fa fas fa-tags" style="position: absolute;left:50px" ></i>
			      
		          <span>标签</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/categories " style="margin-left:75px">
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:50px" ></i>
			      
		          <span>分类</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/blog/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友链
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-user-circle"></i>
			
			关于
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  >
              
                <li>

                  <a href="/blog/about " style="margin-left:75px">
				  
				   <i class="fa fas fa-star-of-david" style="position: absolute;left:50px" ></i>
			      
		          <span>主页</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/gallery " style="margin-left:75px">
				  
				   <i class="fa fas fa-images" style="position: absolute;left:50px" ></i>
			      
		          <span>相册</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    
<script src="/blog/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('请输入访问本文章的密码')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/blog/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('/blog/medias/featureimages/0.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 id="post-title" class="description center-align post-title"></h1>

                    
                        <!-- <script src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11"></script> -->
                        <script>
                            var typedObj = new Typed("#post-title", {
                                strings: [ 'ElasticSearch详解' ],
                                startDelay: 300,
                                typeSpeed: 70,
                                loop: false,
                                backSpeed: 50,
                                showCursor: true
                            });
                        </script>
                    
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/blog/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
        background-color: rgb(255, 255, 255,0.7);
        border-radius: 10px;
        box-shadow: 0 10px 35px 2px rgba(0, 0, 0, .15), 0 5px 15px rgba(0, 0, 0, .07), 0 2px 5px -5px rgba(0, 0, 0, .1) !important;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
        max-height: 480px;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                          <div class="article-tag">
                            <span class="chip bg-color">无标签</span>
                          </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/blog/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" class="post-category">
                                数据库
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                

                

                

                

                
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/blog/libs/prism/prism.css">
        

        
        <!-- 代码块折行 -->
        <style type="text/css">
            code[class*="language-"], pre[class*="language-"] { white-space: pre-wrap !important; }
        </style>
        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <table>
<thead>
<tr>
<th></th>
<th><strong><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#1_Elasticsearch_33">第1章 Elasticsearch概述</a></strong></th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#01_35">01-开篇</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#02_53">02-技术选型</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#03_94">03-教学大纲</a></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td><strong><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#2_Elasticsearch_104">第2章 Elasticsearch入门</a></strong></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#04_106">04-入门-环境准备</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#05RESTful__JSON_152">05-入门-RESTful &amp; JSON</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#06Postman_163">06-入门-Postman客户端工具</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#07_172">07-入门-倒排索引</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#08HTTP_195">08-入门-HTTP-索引-创建</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#09HTTP___241">09-入门-HTTP-索引-查询 &amp; 删除</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#10HTTPPut__Post_316">10-入门-HTTP-文档-创建（Put &amp; Post）</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#11HTTP___392">11-入门-HTTP-查询-主键查询 &amp; 全查询</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#12HTTP_____485">12-入门-HTTP-全量修改 &amp; 局部修改 &amp; 删除</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#13HTTP_____618">13-入门-HTTP-条件查询 &amp; 分页查询 &amp; 查询排序</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#14HTTP___1264">14-入门-HTTP-多条件查询 &amp; 范围查询</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#15HTTP_____1522">15-入门-HTTP-全文检索 &amp; 完全匹配 &amp; 高亮查询</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#16HTTP_1810">16-入门-HTTP-聚合查询</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#17HTTP_2049">17-入门-HTTP-映射关系</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#18JavaAPI_2369">18-入门-JavaAPI-环境准备</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#19JavaAPI_2441">19-入门-JavaAPI-索引-创建</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#20JavaAPI___2486">20-入门-JavaAPI-索引-查询 &amp; 删除</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#21JavaAPI___2569">21-入门-JavaAPI-文档-新增 &amp; 修改</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#22JavaAPI___2727">22-入门-JavaAPI-文档-查询 &amp; 删除</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#23JavaAPI___2799">23-入门-JavaAPI-文档-批量新增 &amp; 批量删除</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#24JavaAPI_2882">24-入门-JavaAPI-文档-高级查询-全量查询</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#25JavaAPI_____2982">25-入门-JavaAPI-文档-高级查询-分页查询 &amp; 条件查询 &amp; 查询排序</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#26JavaAPI___3173">26-入门-JavaAPI-文档-高级查询-组合查询 &amp; 范围查询</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#27JavaAPI___3317">27-入门-JavaAPI-文档-高级查询-模糊查询 &amp; 高亮查询</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#28JavaAPI___3477">28-入门-JavaAPI-文档-高级查询-最大值查询 &amp; 分组查询</a></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td><strong><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#3_Elasticsearch_3587">第3章 Elasticsearch环境</a></strong></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#29_3589">29-环境-简介</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#30Windows_3622">30-环境-Windows集群部署</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#31Linux_3821">31-环境-Linux单节点部署</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#32Linux_3945">32-环境-Linux集群部署</a></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td><strong><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#4_Elasticsearch_4073">第4章 Elasticsearch进阶</a></strong></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#33_4075">33-进阶-核心概念</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#34_4156">34-进阶-系统架构-简介</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#35_4169">35-进阶-单节点集群</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#36_4209">36-进阶-故障转移</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#37_4228">37-进阶-水平扩容</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#38_4288">38-进阶-应对故障</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#39___4323">39-进阶-路由计算 &amp; 分片控制</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#40_4352">40-进阶-数据写流程</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#41_4372">41-进阶-数据读流程</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#42___4378">42-进阶-更新流程 &amp; 批量操作流程</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#43_4428">43-进阶-倒排索引</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#44_4501">44-进阶-文档搜索</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#45_____4557">45-进阶-文档刷新 &amp; 文档刷写 &amp; 文档合并</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#46_4689">46-进阶-文档分析</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#47_5124">47-进阶-文档控制</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#48Kibana_5311">48-进阶-文档展示-Kibana</a></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td><strong><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#5_Elasticsearch_5340">第5章 Elasticsearch集成</a></strong></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#49SpringData_5342">49-框架集成-SpringData-整体介绍</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#50SpringData_5374">50-框架集成-SpringData-代码功能集成</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#51SpringData_5598">51-框架集成-SpringData-集成测试-索引操作</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#52SpringData_5639">52-框架集成-SpringData-集成测试-文档操作</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#53SpringData_5749">53-框架集成-SpringData-集成测试-文档搜索</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#54SparkStreaming_5800">54-框架集成-SparkStreaming-集成</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#55Flink_5931">55-框架集成-Flink-集成</a></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td><strong><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#6_Elasticsearch_6060">第6章 Elasticsearch优化</a></strong></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#56_6062">56-优化-硬件选择</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#57_6084">57-优化-分片策略</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#58_6119">58-优化-路由选择</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#59_6140">59-优化-写入速度优化</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#60_6191">60-优化-内存设置</a></td>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#61_6214">61-优化-重要配置</a></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td><strong><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#7_Elasticsearch_6228">第7章 Elasticsearch面试题</a></strong></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://blog.csdn.net/u011863024/article/details/115721328#62_6230">62-面试题</a></td>
<td></td>
<td></td>
</tr>
</tbody></table>
<h1 id="第1章-Elasticsearch概述"><a href="#第1章-Elasticsearch概述" class="headerlink" title="第1章 Elasticsearch概述"></a>第1章 <a target="_blank" rel="noopener" href="https://so.csdn.net/so/search?q=Elasticsearch&spm=1001.2101.3001.7020">Elasticsearch</a>概述</h1><h2 id="01-开篇"><a href="#01-开篇" class="headerlink" title="01-开篇"></a>01-开篇</h2><p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1hh411D7sb">教学视频</a></p>
<p>结构化数据</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/a421a414ea8fb8e25c466add5b00d31f.png" alt="img"></p>
<p>非结构化数据</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/109d3c62c1f36b4f352170488b516709.png" alt="img"></p>
<p>半结构化数据</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/48742ba6bbdc47e0dae2754027f30a31.png" alt="img"></p>
<h2 id="02-技术选型"><a href="#02-技术选型" class="headerlink" title="02-技术选型"></a>02-技术选型</h2><h3 id="Elasticsearch是什么"><a href="#Elasticsearch是什么" class="headerlink" title="Elasticsearch是什么"></a>Elasticsearch是什么</h3><p>The Elastic Stack,包括Elasticsearch、Kibana、Beats和Logstash（也称为ELK Stack）。能够安全可靠地获取任何来源、任何格式的数据，然后实时地对数据进行搜索、分析和可视化。</p>
<p>Elaticsearch，简称为ES，ES是一个<strong>开源的高扩展的分布式全文搜索引擎</strong>，是整个ElasticStack技术栈的核心。</p>
<p>它可以近乎实时的存储、检索数据；本身扩展性很好，可以扩展到上百台服务器，处理PB级别的数据。</p>
<blockquote>
<p>elastic<br>英 [ɪˈlæstɪk] 美 [ɪˈlæstɪk]<br>n. 橡皮圈(或带);松紧带<br>adj. 橡皮圈(或带)的;有弹性的;有弹力的;灵活的;可改变的;可伸缩的</p>
</blockquote>
<h3 id="全文搜索引擎"><a href="#全文搜索引擎" class="headerlink" title="全文搜索引擎"></a>全文搜索引擎</h3><p>Google，百度类的网站搜索，它们都是根据网页中的关键字生成索引，我们在搜索的时候输入关键字，它们会将该关键字即索引匹配到的所有网页返回；还有常见的项目中应用日志的搜索等等。对于这些非结构化的数据文本，关系型数据库搜索不是能很好的支持。</p>
<p>一般传统数据库，全文检索都实现的很鸡肋，因为一般也没人用数据库存文本字段。进行全文检索需要扫描整个表，如果数据量大的话即使对SQL的语法优化，也收效甚微。建立了索引，但是维护起来也很麻烦，对于insert和update操作都会重新构建索引。</p>
<p>基于以上原因可以分析得出，在一些生产环境中，使用常规的搜索方式，性能是非常差的：</p>
<ul>
<li>搜索的数据对象是大量的非结构化的文本数据。</li>
<li>文件记录量达到数十万或数百万个甚至更多。</li>
<li>支持大量基于交互式文本的查询。</li>
<li>需求非常灵活的全文搜索查询。</li>
<li>对高度相关的搜索结果的有特殊需求，但是没有可用的关系数据库可以满足。</li>
<li>对不同记录类型、非文本数据操作或安全事务处理的需求相对较少的情况。为了解决结构化数据搜索和非结构化数据搜索性能问题，我们就需要专业，健壮，强大的全文搜索引擎。</li>
</ul>
<p><strong>这里说到的全文搜索引擎指的是目前广泛应用的主流搜索引擎</strong>。它的工作原理是计算机索引程序通过扫描文章中的每一个词，对每一个词建立一个索引，指明该词在文章中出现的次数和位置，当用户查询时，检索程序就根据事先建立的索引进行查找，并将查找的结果反馈给用户的检索方式。这个过程类似于通过字典中的检索字表查字的过程。</p>
<h3 id="Elasticsearch应用案例"><a href="#Elasticsearch应用案例" class="headerlink" title="Elasticsearch应用案例"></a>Elasticsearch应用案例</h3><ul>
<li>GitHub:2013年初，抛弃了Solr，采取Elasticsearch来做PB级的搜索。“GitHub使用Elasticsearch搜索20TB的数据，包括13亿文件和1300亿行代码”。</li>
<li>维基百科：启动以Elasticsearch为基础的核心搜索架构</li>
<li>百度：目前广泛使用Elasticsearch作为文本数据分析，采集百度所有服务器上的各类指标数据及用户自定义数据，通过对各种数据进行多维分析展示，辅助定位分析实例异常或业务层面异常。目前覆盖百度内部20多个业务线（包括云分析、网盟、预测、文库、直达号、钱包、风控等），单集群最大100台机器，200个ES节点，每天导入30TB+数据。</li>
<li>新浪：使用Elasticsearch分析处理32亿条实时日志。</li>
<li>阿里：使用Elasticsearch构建日志采集和分析体系。</li>
<li>Stack Overflow：解决Bug问题的网站，全英文，编程人员交流的网站。</li>
</ul>
<h2 id="03-教学大纲"><a href="#03-教学大纲" class="headerlink" title="03-教学大纲"></a>03-教学大纲</h2><ul>
<li>第1章 Elasticsearch概述</li>
<li>第2章 Elasticsearch入门</li>
<li>第3章 Elasticsearch环境</li>
<li>第4章 Elasticsearch进阶</li>
<li>第5章 Elasticsearch集成</li>
<li>第6章 Elasticsearch优化</li>
<li>第7章 Elasticsearch面试题</li>
</ul>
<h1 id="第2章-Elasticsearch入门"><a href="#第2章-Elasticsearch入门" class="headerlink" title="第2章 Elasticsearch入门"></a>第2章 Elasticsearch入门</h1><h2 id="04-入门-环境准备"><a href="#04-入门-环境准备" class="headerlink" title="04-入门-环境准备"></a>04-入门-环境准备</h2><p><a target="_blank" rel="noopener" href="https://www.elastic.co/cn/">官方网址</a></p>
<p><a target="_blank" rel="noopener" href="https://www.elastic.co/guide/index.html">官方文档</a></p>
<p><a target="_blank" rel="noopener" href="https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-8-0">Elasticsearch 7.8.0下载页面</a></p>
<p>Windows版的Elasticsearch压缩包，解压即安装完毕，解压后的Elasticsearch的目录结构如下 ：</p>
<table>
<thead>
<tr>
<th>目录</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>bin</td>
<td>可执行脚本目录</td>
</tr>
<tr>
<td>config</td>
<td>配置目录</td>
</tr>
<tr>
<td>jdk</td>
<td>内置 JDK 目录</td>
</tr>
<tr>
<td>lib</td>
<td>类库</td>
</tr>
<tr>
<td>logs</td>
<td>日志目录</td>
</tr>
<tr>
<td>modules</td>
<td>模块目录</td>
</tr>
<tr>
<td>plugins</td>
<td>插件目录</td>
</tr>
</tbody></table>
<p>解压后，进入bin文件目录，点击elasticsearch.bat文件启动ES服务。</p>
<p>注意：9300端口为Elasticsearch集群间组件的通信端口，9200端口为浏览器访问的http协议<a target="_blank" rel="noopener" href="https://so.csdn.net/so/search?q=RESTful&spm=1001.2101.3001.7020">RESTful</a>端口。</p>
<p>打开浏览器，输入地址：<a href="http://localhost:9200，测试返回结果，返回结果如下：">http://localhost:9200，测试返回结果，返回结果如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
  <span class="token property">"name"</span> <span class="token operator">:</span> <span class="token string">"DESKTOP-LNJQ0VF"</span><span class="token punctuation">,</span>
  <span class="token property">"cluster_name"</span> <span class="token operator">:</span> <span class="token string">"elasticsearch"</span><span class="token punctuation">,</span>
  <span class="token property">"cluster_uuid"</span> <span class="token operator">:</span> <span class="token string">"nCZqBhfdT1-pw8Yas4QU9w"</span><span class="token punctuation">,</span>
  <span class="token property">"version"</span> <span class="token operator">:</span> <span class="token punctuation">&#123;</span>
    <span class="token property">"number"</span> <span class="token operator">:</span> <span class="token string">"7.8.0"</span><span class="token punctuation">,</span>
    <span class="token property">"build_flavor"</span> <span class="token operator">:</span> <span class="token string">"default"</span><span class="token punctuation">,</span>
    <span class="token property">"build_type"</span> <span class="token operator">:</span> <span class="token string">"zip"</span><span class="token punctuation">,</span>
    <span class="token property">"build_hash"</span> <span class="token operator">:</span> <span class="token string">"757314695644ea9a1dc2fecd26d1a43856725e65"</span><span class="token punctuation">,</span>
    <span class="token property">"build_date"</span> <span class="token operator">:</span> <span class="token string">"2020-06-14T19:35:50.234439Z"</span><span class="token punctuation">,</span>
    <span class="token property">"build_snapshot"</span> <span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"lucene_version"</span> <span class="token operator">:</span> <span class="token string">"8.5.1"</span><span class="token punctuation">,</span>
    <span class="token property">"minimum_wire_compatibility_version"</span> <span class="token operator">:</span> <span class="token string">"6.8.0"</span><span class="token punctuation">,</span>
    <span class="token property">"minimum_index_compatibility_version"</span> <span class="token operator">:</span> <span class="token string">"6.0.0-beta1"</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  <span class="token property">"tagline"</span> <span class="token operator">:</span> <span class="token string">"You Know, for Search"</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="05-入门-RESTful-amp-JSON"><a href="#05-入门-RESTful-amp-JSON" class="headerlink" title="05-入门-RESTful &amp; JSON"></a>05-入门-RESTful &amp; JSON</h2><p>REST指的是一组架构约束条件和原则。满足这些约束条件和原则的应用程序或设计就是RESTful。Web应用程序最重要的REST原则是，客户端和服务器之间的交互在请求之间是无状态的。从客户端到服务器的每个请求都必须包含理解请求所必需的信息。如果服务器在请求之间的任何时间点重启，客户端不会得到通知。此外，无状态请求可以由任何可用服务器回答，这十分适合云计算之类的环境。客户端可以缓存数据以改进性能。</p>
<p>在服务器端，应用程序状态和功能可以分为各种资源。资源是一个有趣的概念实体，它向客户端公开。资源的例子有：应用程序对象、数据库记录、算法等等。每个资源都使用URI(Universal Resource Identifier)得到一个唯一的地址。所有资源都共享统一的接口，以便在客户端和服务器之间传输状态。使用的是标准的HTTP方法，比如GET、PUT、POST和DELETE。</p>
<p>在REST样式的Web服务中，每个资源都有一个地址。资源本身都是方法调用的目<br>标，方法列表对所有资源都是一样的。这些方法都是标准方法，包括HTTP GET、POST、PUT、DELETE，还可能包括HEAD和OPTIONS。简单的理解就是，如果想要访问互联网上的资源，就必须向资源所在的服务器发出请求，请求体中必须包含资源的网络路径，以及对资源进行的操作(增删改查)。</p>
<p>REST样式的Web服务若有返回结果，大多数以JSON字符串形式返回。</p>
<h2 id="06-入门-Postman客户端工具"><a href="#06-入门-Postman客户端工具" class="headerlink" title="06-入门-Postman客户端工具"></a>06-入门-<a target="_blank" rel="noopener" href="https://so.csdn.net/so/search?q=Postman&spm=1001.2101.3001.7020">Postman</a>客户端工具</h2><p>如果直接通过浏览器向Elasticsearch服务器发请求，那么需要在发送的请求中包含<br>HTTP标准的方法，而HTTP的大部分特性且仅支持GET和POST方法。所以为了能方便地进行客户端的访问，可以使用Postman软件Postman是一款强大的网页调试工具，提供功能强大的Web API和HTTP请求调试。</p>
<p>软件功能强大，界面简洁明晰、操作方便快捷，设计得很人性化。Postman中文版能够发送任何类型的HTTP请求(GET,HEAD,POST,PUT…)，不仅能够表单提交，且可以附带任意类型请求体。</p>
<p><a target="_blank" rel="noopener" href="https://www.postman.com/downloads/">Postman下载页面</a></p>
<h2 id="07-入门-倒排索引"><a href="#07-入门-倒排索引" class="headerlink" title="07-入门-倒排索引"></a>07-入门-倒排索引</h2><p>正排索引（传统）</p>
<table>
<thead>
<tr>
<th>id</th>
<th>content</th>
</tr>
</thead>
<tbody><tr>
<td>1001</td>
<td>my name is zhang san</td>
</tr>
<tr>
<td>1002</td>
<td>my name is li si</td>
</tr>
</tbody></table>
<p>倒排索引</p>
<table>
<thead>
<tr>
<th>keyword</th>
<th>id</th>
</tr>
</thead>
<tbody><tr>
<td>name</td>
<td>1001, 1002</td>
</tr>
<tr>
<td>zhang</td>
<td>1001</td>
</tr>
</tbody></table>
<p>Elasticsearch是<strong>面向文档型数据库</strong>，一条数据在这里就是一个文档。为了方便大家理解，我们将Elasticsearch里存储文档数据和关系型数据库MySQL存储数据的概念进行一个类比</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/146a779da01f53e7f7a8d53132d3c7cf.png" alt="img"></p>
<p>ES里的Index可以看做一个库，而Types相当于表，Documents则相当于表的行。这里Types的概念已经被逐渐弱化，Elasticsearch6.X中，一个index下已经只能包含一个type，Elasticsearch7.X中,Type的概念已经被删除了。</p>
<h2 id="08-入门-HTTP-索引-创建"><a href="#08-入门-HTTP-索引-创建" class="headerlink" title="08-入门-HTTP-索引-创建"></a>08-入门-HTTP-索引-创建</h2><p>对比关系型数据库，创建索引就等同于创建数据库。</p>
<p>在Postman中，向ES服务器发PUT请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping">http://127.0.0.1:9200/shopping</a></p>
<p>请求后，服务器返回响应：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"acknowledged"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span><span class="token comment">//响应结果</span>
    <span class="token property">"shards_acknowledged"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span><span class="token comment">//分片结果</span>
    <span class="token property">"index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token comment">//索引名称</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台日志：</p>
<pre class="line-numbers language-cmd" data-language="cmd"><code class="language-cmd">[2021-04-08T13:57:06,954][INFO ][o.e.c.m.MetadataCreateIndexService] [DESKTOP-LNJQ0VF] [shopping] creating index, cause [api], templates [], shards [1]&#x2F;[1], mappings []
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>如果重复发PUT请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping">http://127.0.0.1:9200/shopping</a> 添加索引，会返回错误信息:</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"error"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"root_cause"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"resource_already_exists_exception"</span><span class="token punctuation">,</span>
                <span class="token property">"reason"</span><span class="token operator">:</span> <span class="token string">"index [shopping/J0WlEhh4R7aDrfIc3AkwWQ] already exists"</span><span class="token punctuation">,</span>
                <span class="token property">"index_uuid"</span><span class="token operator">:</span> <span class="token string">"J0WlEhh4R7aDrfIc3AkwWQ"</span><span class="token punctuation">,</span>
                <span class="token property">"index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"resource_already_exists_exception"</span><span class="token punctuation">,</span>
        <span class="token property">"reason"</span><span class="token operator">:</span> <span class="token string">"index [shopping/J0WlEhh4R7aDrfIc3AkwWQ] already exists"</span><span class="token punctuation">,</span>
        <span class="token property">"index_uuid"</span><span class="token operator">:</span> <span class="token string">"J0WlEhh4R7aDrfIc3AkwWQ"</span><span class="token punctuation">,</span>
        <span class="token property">"index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"status"</span><span class="token operator">:</span> <span class="token number">400</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="09-入门-HTTP-索引-查询-amp-删除"><a href="#09-入门-HTTP-索引-查询-amp-删除" class="headerlink" title="09-入门-HTTP-索引-查询 &amp; 删除"></a>09-入门-HTTP-索引-查询 &amp; 删除</h2><h3 id="查看所有索引"><a href="#查看所有索引" class="headerlink" title="查看所有索引"></a>查看所有索引</h3><p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/_cat/indices?v">http://127.0.0.1:9200/_cat/indices?v</a></p>
<p>这里请求路径中的_cat表示查看的意思，indices表示索引，所以整体含义就是查看当前ES服务器中的所有索引，就好像MySQL中的show tables的感觉，服务器响应结果如下:</p>
<pre class="line-numbers language-none"><code class="language-none">health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   shopping J0WlEhh4R7aDrfIc3AkwWQ   1   1          0            0       208b           208b
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<table>
<thead>
<tr>
<th>表头</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>health</td>
<td>当前服务器健康状态： green(集群完整) yellow(单点正常、集群不完整) red(单点不正常)</td>
</tr>
<tr>
<td>status</td>
<td>索引打开、关闭状态</td>
</tr>
<tr>
<td>index</td>
<td>索引名</td>
</tr>
<tr>
<td>uuid</td>
<td>索引统一编号</td>
</tr>
<tr>
<td>pri</td>
<td>主分片数量</td>
</tr>
<tr>
<td>rep</td>
<td>副本数量</td>
</tr>
<tr>
<td>docs.count</td>
<td>可用文档数量</td>
</tr>
<tr>
<td>docs.deleted</td>
<td>文档删除状态（逻辑删除）</td>
</tr>
<tr>
<td>store.size</td>
<td>主分片和副分片整体占空间大小</td>
</tr>
<tr>
<td>pri.store.size</td>
<td>主分片占空间大小</td>
</tr>
</tbody></table>
<h3 id="查看单个索引"><a href="#查看单个索引" class="headerlink" title="查看单个索引"></a>查看单个索引</h3><p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping">http://127.0.0.1:9200/shopping</a></p>
<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"shopping"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span><span class="token comment">//索引名</span>
        <span class="token property">"aliases"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token comment">//别名</span>
        <span class="token property">"mappings"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token comment">//映射</span>
        <span class="token property">"settings"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span><span class="token comment">//设置</span>
            <span class="token property">"index"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span><span class="token comment">//设置 - 索引</span>
                <span class="token property">"creation_date"</span><span class="token operator">:</span> <span class="token string">"1617861426847"</span><span class="token punctuation">,</span><span class="token comment">//设置 - 索引 - 创建时间</span>
                <span class="token property">"number_of_shards"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span><span class="token comment">//设置 - 索引 - 主分片数量</span>
                <span class="token property">"number_of_replicas"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span><span class="token comment">//设置 - 索引 - 主分片数量</span>
                <span class="token property">"uuid"</span><span class="token operator">:</span> <span class="token string">"J0WlEhh4R7aDrfIc3AkwWQ"</span><span class="token punctuation">,</span><span class="token comment">//设置 - 索引 - 主分片数量</span>
                <span class="token property">"version"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span><span class="token comment">//设置 - 索引 - 主分片数量</span>
                    <span class="token property">"created"</span><span class="token operator">:</span> <span class="token string">"7080099"</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token property">"provided_name"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token comment">//设置 - 索引 - 主分片数量</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="删除索引"><a href="#删除索引" class="headerlink" title="删除索引"></a>删除索引</h3><p>在Postman中，向ES服务器发DELETE请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping">http://127.0.0.1:9200/shopping</a></p>
<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"acknowledged"</span><span class="token operator">:</span> <span class="token boolean">true</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>再次查看所有索引，GET <a target="_blank" rel="noopener" href="http://127.0.0.1:9200/_cat/indices?v%EF%BC%8C%E8%BF%94%E5%9B%9E%E7%BB%93%E6%9E%9C%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/_cat/indices?v，返回结果如下：</a></p>
<pre class="line-numbers language-none"><code class="language-none">health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>成功删除。</p>
<h2 id="10-入门-HTTP-文档-创建（Put-amp-Post）"><a href="#10-入门-HTTP-文档-创建（Put-amp-Post）" class="headerlink" title="10-入门-HTTP-文档-创建（Put &amp; Post）"></a>10-入门-HTTP-文档-创建（Put &amp; Post）</h2><p>假设索引已经创建好了，接下来我们来创建文档，并添加数据。这里的文档可以类比为关系型数据库中的表数据，添加的数据格式为JSON格式</p>
<p>在Postman中，向ES服务器发POST请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_doc%EF%BC%8C%E8%AF%B7%E6%B1%82%E4%BD%93JSON%E5%86%85%E5%AE%B9%E4%B8%BA%EF%BC%9A">http://127.0.0.1:9200/shopping/_doc，请求体JSON内容为：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"小米手机"</span><span class="token punctuation">,</span>
    <span class="token property">"category"</span><span class="token operator">:</span><span class="token string">"小米"</span><span class="token punctuation">,</span>
    <span class="token property">"images"</span><span class="token operator">:</span><span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
    <span class="token property">"price"</span><span class="token operator">:</span><span class="token number">3999.00</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://img-blog.csdnimg.cn/img_convert/20d54cba223bd9d70ea356d3e40a8161.png" alt="img"></p>
<p>注意，此处发送请求的方式必须为POST，不能是PUT，否则会发生错误。</p>
<p>返回结果：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span><span class="token comment">//索引</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span><span class="token comment">//类型-文档</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span><span class="token comment">//唯一标识，可以类比为 MySQL 中的主键，随机生成</span>
    <span class="token property">"_version"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token comment">//版本</span>
    <span class="token property">"result"</span><span class="token operator">:</span> <span class="token string">"created"</span><span class="token punctuation">,</span><span class="token comment">//结果，这里的 create 表示创建成功</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span><span class="token comment">//</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span><span class="token comment">//分片 - 总数</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token comment">//分片 - 总数</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token comment">//分片 - 总数</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"_seq_no"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    <span class="token property">"_primary_term"</span><span class="token operator">:</span> <span class="token number">1</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>上面的数据创建后，由于没有指定数据唯一性标识（ID），默认情况下，ES服务器会随机生成一个。</p>
<p>如果想要自定义唯一性标识，需要在创建时指定： <a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_doc/1%EF%BC%8C%E8%AF%B7%E6%B1%82%E4%BD%93JSON%E5%86%85%E5%AE%B9%E4%B8%BA%EF%BC%9A">http://127.0.0.1:9200/shopping/_doc/1，请求体JSON内容为：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"小米手机"</span><span class="token punctuation">,</span>
    <span class="token property">"category"</span><span class="token operator">:</span><span class="token string">"小米"</span><span class="token punctuation">,</span>
    <span class="token property">"images"</span><span class="token operator">:</span><span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
    <span class="token property">"price"</span><span class="token operator">:</span><span class="token number">3999.00</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span><span class="token comment">//&lt;------------------自定义唯一性标识</span>
    <span class="token property">"_version"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">"result"</span><span class="token operator">:</span> <span class="token string">"created"</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"_seq_no"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">"_primary_term"</span><span class="token operator">:</span> <span class="token number">1</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><strong>此处需要注意：如果增加数据时明确数据主键，那么请求方式也可以为PUT。</strong></p>
<h2 id="11-入门-HTTP-查询-主键查询-amp-全查询"><a href="#11-入门-HTTP-查询-主键查询-amp-全查询" class="headerlink" title="11-入门-HTTP-查询-主键查询 &amp; 全查询"></a>11-入门-HTTP-查询-<a target="_blank" rel="noopener" href="https://so.csdn.net/so/search?q=%E4%B8%BB%E9%94%AE&spm=1001.2101.3001.7020">主键</a>查询 &amp; 全查询</h2><p>查看文档时，需要指明文档的唯一性标识，类似于MySQL中数据的主键查询</p>
<p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_doc/1%E3%80%82">http://127.0.0.1:9200/shopping/_doc/1。</a></p>
<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span>
    <span class="token property">"_version"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">"_seq_no"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">"_primary_term"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">"found"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
        <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
        <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
        <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查找不存在的内容，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_doc/1001%E3%80%82">http://127.0.0.1:9200/shopping/_doc/1001。</a></p>
<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1001"</span><span class="token punctuation">,</span>
    <span class="token property">"found"</span><span class="token operator">:</span> <span class="token boolean">false</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查看索引下所有数据，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%E3%80%82">http://127.0.0.1:9200/shopping/_search。</a></p>
<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">133</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="12-入门-HTTP-全量修改-amp-局部修改-amp-删除"><a href="#12-入门-HTTP-全量修改-amp-局部修改-amp-删除" class="headerlink" title="12-入门-HTTP-全量修改 &amp; 局部修改 &amp; 删除"></a>12-入门-HTTP-全量修改 &amp; 局部修改 &amp; 删除</h2><h3 id="全量修改"><a href="#全量修改" class="headerlink" title="全量修改"></a>全量修改</h3><p>和新增文档一样，输入相同的URL地址请求，如果请求体变化，会将原有的数据内容覆盖</p>
<p>在Postman中，向ES服务器发POST请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_doc/1">http://127.0.0.1:9200/shopping/_doc/1</a></p>
<p>请求体JSON内容为:</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"华为手机"</span><span class="token punctuation">,</span>
    <span class="token property">"category"</span><span class="token operator">:</span><span class="token string">"华为"</span><span class="token punctuation">,</span>
    <span class="token property">"images"</span><span class="token operator">:</span><span class="token string">"http://www.gulixueyuan.com/hw.jpg"</span><span class="token punctuation">,</span>
    <span class="token property">"price"</span><span class="token operator">:</span><span class="token number">1999.00</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>修改成功后，服务器响应结果：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span>
    <span class="token property">"_version"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
    <span class="token property">"result"</span><span class="token operator">:</span> <span class="token string">"updated"</span><span class="token punctuation">,</span><span class="token comment">//&lt;-----------updated 表示数据被更新</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"_seq_no"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
    <span class="token property">"_primary_term"</span><span class="token operator">:</span> <span class="token number">1</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="局部修改"><a href="#局部修改" class="headerlink" title="局部修改"></a>局部修改</h3><p>修改数据时，也可以只修改某一给条数据的局部信息</p>
<p>在Postman中，向ES服务器发POST请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_update/1%E3%80%82">http://127.0.0.1:9200/shopping/_update/1。</a></p>
<p>请求体JSON内容为:</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"doc"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
		<span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"小米手机"</span><span class="token punctuation">,</span>
		<span class="token property">"category"</span><span class="token operator">:</span><span class="token string">"小米"</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span>
    <span class="token property">"_version"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
    <span class="token property">"result"</span><span class="token operator">:</span> <span class="token string">"updated"</span><span class="token punctuation">,</span><span class="token comment">//&lt;-----------updated 表示数据被更新</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"_seq_no"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
    <span class="token property">"_primary_term"</span><span class="token operator">:</span> <span class="token number">1</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_doc/1%EF%BC%8C%E6%9F%A5%E7%9C%8B%E4%BF%AE%E6%94%B9%E5%86%85%E5%AE%B9%EF%BC%9A">http://127.0.0.1:9200/shopping/_doc/1，查看修改内容：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span>
    <span class="token property">"_version"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
    <span class="token property">"_seq_no"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
    <span class="token property">"_primary_term"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">"found"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
        <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
        <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/hw.jpg"</span><span class="token punctuation">,</span>
        <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h3><p>删除一个文档不会立即从磁盘上移除，它只是被标记成已删除（逻辑删除）。</p>
<p>在Postman中，向ES服务器发DELETE请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_doc/1">http://127.0.0.1:9200/shopping/_doc/1</a></p>
<p>返回结果：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span>
    <span class="token property">"_version"</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
    <span class="token property">"result"</span><span class="token operator">:</span> <span class="token string">"deleted"</span><span class="token punctuation">,</span><span class="token comment">//&lt;---删除成功</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"_seq_no"</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
    <span class="token property">"_primary_term"</span><span class="token operator">:</span> <span class="token number">1</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_doc/1%EF%BC%8C%E6%9F%A5%E7%9C%8B%E6%98%AF%E5%90%A6%E5%88%A0%E9%99%A4%E6%88%90%E5%8A%9F%EF%BC%9A">http://127.0.0.1:9200/shopping/_doc/1，查看是否删除成功：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span>
    <span class="token property">"found"</span><span class="token operator">:</span> <span class="token boolean">false</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="13-入门-HTTP-条件查询-amp-分页查询-amp-查询排序"><a href="#13-入门-HTTP-条件查询-amp-分页查询-amp-查询排序" class="headerlink" title="13-入门-HTTP-条件查询 &amp; 分页查询 &amp; 查询排序"></a>13-入门-HTTP-条件查询 &amp; 分页查询 &amp; 查询排序</h2><h3 id="条件查询"><a href="#条件查询" class="headerlink" title="条件查询"></a>条件查询</h3><p>假设有以下文档内容，（在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%89%EF%BC%9A">http://127.0.0.1:9200/shopping/_search）：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"A9R5sHgBaKNfVnMb25Ya"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BNR5sHgBaKNfVnMb7pal"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BtR6sHgBaKNfVnMbX5Y5"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"B9R6sHgBaKNfVnMbZpZ6"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"CdR7sHgBaKNfVnMbsJb9"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="URL带参查询"><a href="#URL带参查询" class="headerlink" title="URL带参查询"></a>URL带参查询</h4><p><strong>查找category为小米的文档</strong>，在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search?q=category:%E5%B0%8F%E7%B1%B3%EF%BC%8C%E8%BF%94%E5%9B%9E%E7%BB%93%E6%9E%9C%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search?q=category:小米，返回结果如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">94</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"A9R5sHgBaKNfVnMb25Ya"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BNR5sHgBaKNfVnMb7pal"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>上述为URL带参数形式查询，这很容易让不善者心怀恶意，或者参数值出现中文会出现乱码情况。为了避免这些情况，我们可用使用带JSON请求体请求进行查询。</p>
<h4 id="请求体带参查询"><a href="#请求体带参查询" class="headerlink" title="请求体带参查询"></a>请求体带参查询</h4><p>接下带JSON请求体，还是<strong>查找category为小米的文档</strong>，在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%8C%E9%99%84%E5%B8%A6JSON%E4%BD%93%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search，附带JSON体如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"match"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
			<span class="token property">"category"</span><span class="token operator">:</span><span class="token string">"小米"</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"A9R5sHgBaKNfVnMb25Ya"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BNR5sHgBaKNfVnMb7pal"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="带请求体方式的查找所有内容"><a href="#带请求体方式的查找所有内容" class="headerlink" title="带请求体方式的查找所有内容"></a>带请求体方式的查找所有内容</h4><p><strong>查找所有文档内容</strong>，也可以这样，在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%8C%E9%99%84%E5%B8%A6JSON%E4%BD%93%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search，附带JSON体如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"match_all"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>则返回所有文档内容：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"A9R5sHgBaKNfVnMb25Ya"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BNR5sHgBaKNfVnMb7pal"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BtR6sHgBaKNfVnMbX5Y5"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"B9R6sHgBaKNfVnMbZpZ6"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"CdR7sHgBaKNfVnMbsJb9"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="查询指定字段"><a href="#查询指定字段" class="headerlink" title="查询指定字段"></a>查询指定字段</h4><p><strong>如果你想查询指定字段</strong>，在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%8C%E9%99%84%E5%B8%A6JSON%E4%BD%93%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search，附带JSON体如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"match_all"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
	<span class="token property">"_source"</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token string">"title"</span><span class="token punctuation">]</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"A9R5sHgBaKNfVnMb25Ya"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BNR5sHgBaKNfVnMb7pal"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BtR6sHgBaKNfVnMbX5Y5"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"B9R6sHgBaKNfVnMbZpZ6"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"CdR7sHgBaKNfVnMbsJb9"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="分页查询"><a href="#分页查询" class="headerlink" title="分页查询"></a>分页查询</h3><p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%8C%E9%99%84%E5%B8%A6JSON%E4%BD%93%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search，附带JSON体如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"match_all"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
	<span class="token property">"from"</span><span class="token operator">:</span><span class="token number">0</span><span class="token punctuation">,</span>
	<span class="token property">"size"</span><span class="token operator">:</span><span class="token number">2</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token punctuation">&#123;</span>
    <span class="token string">"took"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token string">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token string">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token string">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token string">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token string">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token string">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token string">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token string">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token string">"value"</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
            <span class="token string">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token string">"max_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token string">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token string">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token string">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token string">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span>
                <span class="token string">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token string">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token string">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token string">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token string">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token string">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token string">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token string">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token string">"_id"</span><span class="token operator">:</span> <span class="token string">"A9R5sHgBaKNfVnMb25Ya"</span><span class="token punctuation">,</span>
                <span class="token string">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token string">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token string">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token string">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token string">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token string">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="查询排序"><a href="#查询排序" class="headerlink" title="查询排序"></a>查询排序</h3><p>如果你想通过排序查出价格最高的手机，在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%8C%E9%99%84%E5%B8%A6JSON%E4%BD%93%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search，附带JSON体如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"match_all"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
	<span class="token property">"sort"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"price"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
			<span class="token property">"order"</span><span class="token operator">:</span><span class="token string">"desc"</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">96</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token null keyword">null</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token null keyword">null</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token property">"sort"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                    <span class="token number">3999</span>
                <span class="token punctuation">]</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"A9R5sHgBaKNfVnMb25Ya"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token null keyword">null</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token property">"sort"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                    <span class="token number">1999</span>
                <span class="token punctuation">]</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BNR5sHgBaKNfVnMb7pal"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token null keyword">null</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token property">"sort"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                    <span class="token number">1999</span>
                <span class="token punctuation">]</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BtR6sHgBaKNfVnMbX5Y5"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token null keyword">null</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token property">"sort"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                    <span class="token number">1999</span>
                <span class="token punctuation">]</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"B9R6sHgBaKNfVnMbZpZ6"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token null keyword">null</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token property">"sort"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                    <span class="token number">1999</span>
                <span class="token punctuation">]</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"CdR7sHgBaKNfVnMbsJb9"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token null keyword">null</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token property">"sort"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                    <span class="token number">1999</span>
                <span class="token punctuation">]</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="14-入门-HTTP-多条件查询-amp-范围查询"><a href="#14-入门-HTTP-多条件查询-amp-范围查询" class="headerlink" title="14-入门-HTTP-多条件查询 &amp; 范围查询"></a>14-入门-HTTP-多条件查询 &amp; 范围查询</h2><h3 id="多条件查询"><a href="#多条件查询" class="headerlink" title="多条件查询"></a>多条件查询</h3><p>假设想找出小米牌子，价格为3999元的。（must相当于数据库的&amp;&amp;）</p>
<p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%8C%E9%99%84%E5%B8%A6JSON%E4%BD%93%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search，附带JSON体如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"bool"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
			<span class="token property">"must"</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token punctuation">&#123;</span>
				<span class="token property">"match"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
					<span class="token property">"category"</span><span class="token operator">:</span><span class="token string">"小米"</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token punctuation">&#123;</span>
				<span class="token property">"match"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
					<span class="token property">"price"</span><span class="token operator">:</span><span class="token number">3999.00</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span><span class="token punctuation">]</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">134</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">2.3862944</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">2.3862944</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>假设想找出小米和华为的牌子。（should相当于数据库的||）</p>
<p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%8C%E9%99%84%E5%B8%A6JSON%E4%BD%93%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search，附带JSON体如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"bool"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
			<span class="token property">"should"</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token punctuation">&#123;</span>
				<span class="token property">"match"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
					<span class="token property">"category"</span><span class="token operator">:</span><span class="token string">"小米"</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token punctuation">&#123;</span>
				<span class="token property">"match"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
					<span class="token property">"category"</span><span class="token operator">:</span><span class="token string">"华为"</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span><span class="token punctuation">]</span>
		<span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"filter"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
            <span class="token property">"range"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
                <span class="token property">"price"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
                    <span class="token property">"gt"</span><span class="token operator">:</span><span class="token number">2000</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">8</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"A9R5sHgBaKNfVnMb25Ya"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BNR5sHgBaKNfVnMb7pal"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BtR6sHgBaKNfVnMbX5Y5"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"B9R6sHgBaKNfVnMbZpZ6"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"CdR7sHgBaKNfVnMbsJb9"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="范围查询"><a href="#范围查询" class="headerlink" title="范围查询"></a>范围查询</h3><p>假设想找出小米和华为的牌子，价格大于2000元的手机。</p>
<p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%8C%E9%99%84%E5%B8%A6JSON%E4%BD%93%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search，附带JSON体如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"bool"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
			<span class="token property">"should"</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token punctuation">&#123;</span>
				<span class="token property">"match"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
					<span class="token property">"category"</span><span class="token operator">:</span><span class="token string">"小米"</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token punctuation">&#123;</span>
				<span class="token property">"match"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
					<span class="token property">"category"</span><span class="token operator">:</span><span class="token string">"华为"</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"filter"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
            	<span class="token property">"range"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
                	<span class="token property">"price"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
                    	<span class="token property">"gt"</span><span class="token operator">:</span><span class="token number">2000</span>
                	<span class="token punctuation">&#125;</span>
	            <span class="token punctuation">&#125;</span>
    	    <span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">72</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.3862942</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="15-入门-HTTP-全文检索-amp-完全匹配-amp-高亮查询"><a href="#15-入门-HTTP-全文检索-amp-完全匹配-amp-高亮查询" class="headerlink" title="15-入门-HTTP-全文检索 &amp; 完全匹配 &amp; 高亮查询"></a>15-入门-HTTP-全文检索 &amp; 完全匹配 &amp; 高亮查询</h2><h3 id="全文检索"><a href="#全文检索" class="headerlink" title="全文检索"></a>全文检索</h3><p>这功能像搜索引擎那样，如品牌输入“小华”，返回结果带回品牌有“小米”和华为的。</p>
<p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%8C%E9%99%84%E5%B8%A6JSON%E4%BD%93%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search，附带JSON体如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"match"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
			<span class="token property">"category"</span> <span class="token operator">:</span> <span class="token string">"小华"</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">7</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"A9R5sHgBaKNfVnMb25Ya"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BNR5sHgBaKNfVnMb7pal"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BtR6sHgBaKNfVnMbX5Y5"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"B9R6sHgBaKNfVnMbZpZ6"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"CdR7sHgBaKNfVnMbsJb9"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="完全匹配"><a href="#完全匹配" class="headerlink" title="完全匹配"></a>完全匹配</h3><p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%8C%E9%99%84%E5%B8%A6JSON%E4%BD%93%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search，附带JSON体如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"match_phrase"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
			<span class="token property">"category"</span> <span class="token operator">:</span> <span class="token string">"为"</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BtR6sHgBaKNfVnMbX5Y5"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"B9R6sHgBaKNfVnMbZpZ6"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"CdR7sHgBaKNfVnMbsJb9"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="高亮查询"><a href="#高亮查询" class="headerlink" title="高亮查询"></a>高亮查询</h3><p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%8C%E9%99%84%E5%B8%A6JSON%E4%BD%93%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search，附带JSON体如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"match_phrase"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
			<span class="token property">"category"</span> <span class="token operator">:</span> <span class="token string">"为"</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"highlight"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
        <span class="token property">"fields"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
            <span class="token property">"category"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token comment">//&lt;----高亮这字段</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">100</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BtR6sHgBaKNfVnMbX5Y5"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token property">"highlight"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                        <span class="token string">"华&lt;em>为&lt;/em>"</span><span class="token comment">//&lt;------高亮一个为字。</span>
                    <span class="token punctuation">]</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"B9R6sHgBaKNfVnMbZpZ6"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token property">"highlight"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                        <span class="token string">"华&lt;em>为&lt;/em>"</span>
                    <span class="token punctuation">]</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"CdR7sHgBaKNfVnMbsJb9"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.6931471</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token property">"highlight"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                        <span class="token string">"华&lt;em>为&lt;/em>"</span>
                    <span class="token punctuation">]</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="16-入门-HTTP-聚合查询"><a href="#16-入门-HTTP-聚合查询" class="headerlink" title="16-入门-HTTP-聚合查询"></a>16-入门-HTTP-聚合查询</h2><p>聚合允许使用者对es文档进行统计分析，类似与关系型数据库中的groupby，当然还有很多其他的聚合，例如取最大值max、平均值avg等等。</p>
<p>接下来按price字段进行分组：</p>
<p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%8C%E9%99%84%E5%B8%A6JSON%E4%BD%93%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search，附带JSON体如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"aggs"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span><span class="token comment">//聚合操作</span>
		<span class="token property">"price_group"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span><span class="token comment">//名称，随意起名</span>
			<span class="token property">"terms"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span><span class="token comment">//分组</span>
				<span class="token property">"field"</span><span class="token operator">:</span><span class="token string">"price"</span><span class="token comment">//分组字段</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">63</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"ANQqsHgBaKNfVnMbhZYU"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">3999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"A9R5sHgBaKNfVnMb25Ya"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BNR5sHgBaKNfVnMb7pal"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"小米手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"BtR6sHgBaKNfVnMbX5Y5"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"B9R6sHgBaKNfVnMbZpZ6"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"CdR7sHgBaKNfVnMbsJb9"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"华为手机"</span><span class="token punctuation">,</span>
                    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"华为"</span><span class="token punctuation">,</span>
                    <span class="token property">"images"</span><span class="token operator">:</span> <span class="token string">"http://www.gulixueyuan.com/xm.jpg"</span><span class="token punctuation">,</span>
                    <span class="token property">"price"</span><span class="token operator">:</span> <span class="token number">1999</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"aggregations"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"price_group"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"doc_count_error_upper_bound"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
            <span class="token property">"sum_other_doc_count"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
            <span class="token property">"buckets"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token punctuation">&#123;</span>
                    <span class="token property">"key"</span><span class="token operator">:</span> <span class="token number">1999</span><span class="token punctuation">,</span>
                    <span class="token property">"doc_count"</span><span class="token operator">:</span> <span class="token number">5</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token punctuation">&#123;</span>
                    <span class="token property">"key"</span><span class="token operator">:</span> <span class="token number">3999</span><span class="token punctuation">,</span>
                    <span class="token property">"doc_count"</span><span class="token operator">:</span> <span class="token number">1</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">]</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>上面返回结果会附带原始数据的。若不想要不附带原始数据的结果，在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search">http://127.0.0.1:9200/shopping/_search</a> 附带JSON体如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"aggs"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"price_group"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
			<span class="token property">"terms"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
				<span class="token property">"field"</span><span class="token operator">:</span><span class="token string">"price"</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"size"</span><span class="token operator">:</span><span class="token number">0</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">60</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token null keyword">null</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"aggregations"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"price_group"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"doc_count_error_upper_bound"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
            <span class="token property">"sum_other_doc_count"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
            <span class="token property">"buckets"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token punctuation">&#123;</span>
                    <span class="token property">"key"</span><span class="token operator">:</span> <span class="token number">1999</span><span class="token punctuation">,</span>
                    <span class="token property">"doc_count"</span><span class="token operator">:</span> <span class="token number">5</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token punctuation">&#123;</span>
                    <span class="token property">"key"</span><span class="token operator">:</span> <span class="token number">3999</span><span class="token punctuation">,</span>
                    <span class="token property">"doc_count"</span><span class="token operator">:</span> <span class="token number">1</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">]</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>若想对所有手机价格求<strong>平均值</strong>。</p>
<p>在Postman中，向ES服务器发GET请求：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_search%EF%BC%8C%E9%99%84%E5%B8%A6JSON%E4%BD%93%E5%A6%82%E4%B8%8B%EF%BC%9A">http://127.0.0.1:9200/shopping/_search，附带JSON体如下：</a></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
	<span class="token property">"aggs"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"price_avg"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span><span class="token comment">//名称，随意起名</span>
			<span class="token property">"avg"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span><span class="token comment">//求平均</span>
				<span class="token property">"field"</span><span class="token operator">:</span><span class="token string">"price"</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"size"</span><span class="token operator">:</span><span class="token number">0</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">14</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token null keyword">null</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"aggregations"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"price_avg"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">2332.3333333333335</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="17-入门-HTTP-映射关系"><a href="#17-入门-HTTP-映射关系" class="headerlink" title="17-入门-HTTP-映射关系"></a>17-入门-HTTP-映射关系</h2><p>有了索引库，等于有了数据库中的database。</p>
<p>接下来就需要建索引库(index)中的映射了，类似于数据库(database)中的表结构(table)。</p>
<p>创建数据库表需要设置字段名称，类型，长度，约束等；索引库也一样，需要知道这个类型下有哪些字段，每个字段有哪些约束信息，这就叫做映射(mapping)。</p>
<p>先创建一个索引：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"># PUT http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/user</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>返回结果：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"acknowledged"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    <span class="token property">"shards_acknowledged"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    <span class="token property">"index"</span><span class="token operator">:</span> <span class="token string">"user"</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><strong>创建映射</strong></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"># PUT http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/user/_mapping</span>

<span class="token punctuation">&#123;</span>
    <span class="token property">"properties"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"name"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
        	<span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"text"</span><span class="token punctuation">,</span>
        	<span class="token property">"index"</span><span class="token operator">:</span> <span class="token boolean">true</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"sex"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
        	<span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"keyword"</span><span class="token punctuation">,</span>
        	<span class="token property">"index"</span><span class="token operator">:</span> <span class="token boolean">true</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"tel"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
        	<span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"keyword"</span><span class="token punctuation">,</span>
        	<span class="token property">"index"</span><span class="token operator">:</span> <span class="token boolean">false</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"acknowledged"</span><span class="token operator">:</span> <span class="token boolean">true</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p><strong>查询映射</strong></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#GET http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/user/_mapping</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"user"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"mappings"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"properties"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                <span class="token property">"name"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"text"</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token property">"sex"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"keyword"</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token property">"tel"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"keyword"</span><span class="token punctuation">,</span>
                    <span class="token property">"index"</span><span class="token operator">:</span> <span class="token boolean">false</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>增加数据</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#PUT http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/user/_create/1001</span>
<span class="token punctuation">&#123;</span>
	<span class="token property">"name"</span><span class="token operator">:</span><span class="token string">"小米"</span><span class="token punctuation">,</span>
	<span class="token property">"sex"</span><span class="token operator">:</span><span class="token string">"男的"</span><span class="token punctuation">,</span>
	<span class="token property">"tel"</span><span class="token operator">:</span><span class="token string">"1111"</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1001"</span><span class="token punctuation">,</span>
    <span class="token property">"_version"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">"result"</span><span class="token operator">:</span> <span class="token string">"created"</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"_seq_no"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    <span class="token property">"_primary_term"</span><span class="token operator">:</span> <span class="token number">1</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查找name含有”小“数据：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#GET http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/user/_search</span>
<span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"match"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
			<span class="token property">"name"</span><span class="token operator">:</span><span class="token string">"小"</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">495</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">0.2876821</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1001"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.2876821</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"sex"</span><span class="token operator">:</span> <span class="token string">"男的"</span><span class="token punctuation">,</span>
                    <span class="token property">"tel"</span><span class="token operator">:</span> <span class="token string">"1111"</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查找sex含有”男“数据：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#GET http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/user/_search</span>
<span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"match"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
			<span class="token property">"sex"</span><span class="token operator">:</span><span class="token string">"男"</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token null keyword">null</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>找不想要的结果，只因创建映射时”sex”的类型为”keyword”。</p>
<p>“sex”只能完全为”男的“，才能得出原数据。</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#GET http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/user/_search</span>
<span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"match"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
			<span class="token property">"sex"</span><span class="token operator">:</span><span class="token string">"男的"</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">0.2876821</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1001"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">0.2876821</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"小米"</span><span class="token punctuation">,</span>
                    <span class="token property">"sex"</span><span class="token operator">:</span> <span class="token string">"男的"</span><span class="token punctuation">,</span>
                    <span class="token property">"tel"</span><span class="token operator">:</span> <span class="token string">"1111"</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查询电话</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"># GET http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/user/_search</span>
<span class="token punctuation">&#123;</span>
	<span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
		<span class="token property">"match"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
			<span class="token property">"tel"</span><span class="token operator">:</span><span class="token string">"11"</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"error"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"root_cause"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"query_shard_exception"</span><span class="token punctuation">,</span>
                <span class="token property">"reason"</span><span class="token operator">:</span> <span class="token string">"failed to create query: Cannot search on field [tel] since it is not indexed."</span><span class="token punctuation">,</span>
                <span class="token property">"index_uuid"</span><span class="token operator">:</span> <span class="token string">"ivLnMfQKROS7Skb2MTFOew"</span><span class="token punctuation">,</span>
                <span class="token property">"index"</span><span class="token operator">:</span> <span class="token string">"user"</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"search_phase_execution_exception"</span><span class="token punctuation">,</span>
        <span class="token property">"reason"</span><span class="token operator">:</span> <span class="token string">"all shards failed"</span><span class="token punctuation">,</span>
        <span class="token property">"phase"</span><span class="token operator">:</span> <span class="token string">"query"</span><span class="token punctuation">,</span>
        <span class="token property">"grouped"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
        <span class="token property">"failed_shards"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"shard"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
                <span class="token property">"index"</span><span class="token operator">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span>
                <span class="token property">"node"</span><span class="token operator">:</span> <span class="token string">"4P7dIRfXSbezE5JTiuylew"</span><span class="token punctuation">,</span>
                <span class="token property">"reason"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"query_shard_exception"</span><span class="token punctuation">,</span>
                    <span class="token property">"reason"</span><span class="token operator">:</span> <span class="token string">"failed to create query: Cannot search on field [tel] since it is not indexed."</span><span class="token punctuation">,</span>
                    <span class="token property">"index_uuid"</span><span class="token operator">:</span> <span class="token string">"ivLnMfQKROS7Skb2MTFOew"</span><span class="token punctuation">,</span>
                    <span class="token property">"index"</span><span class="token operator">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span>
                    <span class="token property">"caused_by"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                        <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"illegal_argument_exception"</span><span class="token punctuation">,</span>
                        <span class="token property">"reason"</span><span class="token operator">:</span> <span class="token string">"Cannot search on field [tel] since it is not indexed."</span>
                    <span class="token punctuation">&#125;</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"status"</span><span class="token operator">:</span> <span class="token number">400</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>报错只因创建映射时”tel”的”index”为false。</p>
<h2 id="18-入门-JavaAPI-环境准备"><a href="#18-入门-JavaAPI-环境准备" class="headerlink" title="18-入门-JavaAPI-环境准备"></a>18-入门-JavaAPI-环境准备</h2><p>新建Maven工程。</p>
<p>添加依赖：</p>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.elasticsearch<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>elasticsearch<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>7.8.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token comment">&lt;!-- elasticsearch的客户端--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.elasticsearch.client<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>elasticsearch-rest-high-level-client<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>7.8.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token comment">&lt;!-- elasticsearch依赖2.x的log4j--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.logging.log4j<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>log4j-api<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.8.2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.logging.log4j<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>log4j-core<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.8.2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.fasterxml.jackson.core<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>jackson-databind<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.9.9<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token comment">&lt;!-- junit单元测试 --></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>4.12<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>HelloElasticsearch</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpHost</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestClient</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestHighLevelClient</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HelloElasticsearch</span> <span class="token punctuation">&#123;</span>

	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
		<span class="token comment">// 创建客户端对象</span>
		<span class="token class-name">RestHighLevelClient</span> client <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
				<span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">"localhost"</span><span class="token punctuation">,</span> <span class="token number">9200</span><span class="token punctuation">,</span> <span class="token string">"http"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//		...</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>client<span class="token punctuation">)</span><span class="token punctuation">;</span>

		<span class="token comment">// 关闭客户端连接</span>
		client<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="19-入门-JavaAPI-索引-创建"><a href="#19-入门-JavaAPI-索引-创建" class="headerlink" title="19-入门-JavaAPI-索引-创建"></a>19-入门-JavaAPI-索引-创建</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpHost</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>admin<span class="token punctuation">.</span>indices<span class="token punctuation">.</span>create<span class="token punctuation">.</span></span><span class="token class-name">CreateIndexRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>admin<span class="token punctuation">.</span>indices<span class="token punctuation">.</span>create<span class="token punctuation">.</span></span><span class="token class-name">CreateIndexResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestClient</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestHighLevelClient</span></span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">CreateIndex</span> <span class="token punctuation">&#123;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 创建客户端对象</span>
        <span class="token class-name">RestHighLevelClient</span> client <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
                <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">"localhost"</span><span class="token punctuation">,</span> <span class="token number">9200</span><span class="token punctuation">,</span> <span class="token string">"http"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 创建索引 - 请求对象</span>
        <span class="token class-name">CreateIndexRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">CreateIndexRequest</span><span class="token punctuation">(</span><span class="token string">"user2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 发送请求，获取响应</span>
        <span class="token class-name">CreateIndexResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span>
                <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">boolean</span> acknowledged <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">isAcknowledged</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 响应状态</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"操作状态 = "</span> <span class="token operator">+</span> acknowledged<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 关闭客户端连接</span>
        client<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印：</p>
<pre class="line-numbers language-\" data-language="\"><code class="language-\">四月 09, 2021 2:12:08 下午 org.elasticsearch.client.RestClient logResponse
警告: request [PUT http:&#x2F;&#x2F;localhost:9200&#x2F;user2?master_timeout&#x3D;30s&amp;include_type_name&#x3D;true&amp;timeout&#x3D;30s] returned 1 warnings: [299 Elasticsearch-7.8.0-757314695644ea9a1dc2fecd26d1a43856725e65 &quot;[types removal] Using include_type_name in create index requests is deprecated. The parameter will be removed in the next major version.&quot;]
操作状态 &#x3D; true

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="20-入门-JavaAPI-索引-查询-amp-删除"><a href="#20-入门-JavaAPI-索引-查询-amp-删除" class="headerlink" title="20-入门-JavaAPI-索引-查询 &amp; 删除"></a>20-入门-JavaAPI-索引-查询 &amp; 删除</h2><h3 id="查询"><a href="#查询" class="headerlink" title="查询"></a>查询</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpHost</span></span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestClient</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestHighLevelClient</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span>indices<span class="token punctuation">.</span></span><span class="token class-name">GetIndexRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span>indices<span class="token punctuation">.</span></span><span class="token class-name">GetIndexResponse</span></span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SearchIndex</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 创建客户端对象</span>
        <span class="token class-name">RestHighLevelClient</span> client <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
                <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">"localhost"</span><span class="token punctuation">,</span> <span class="token number">9200</span><span class="token punctuation">,</span> <span class="token string">"http"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 查询索引 - 请求对象</span>
        <span class="token class-name">GetIndexRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">GetIndexRequest</span><span class="token punctuation">(</span><span class="token string">"user2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 发送请求，获取响应</span>
        <span class="token class-name">GetIndexResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span>
                <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"aliases:"</span><span class="token operator">+</span>response<span class="token punctuation">.</span><span class="token function">getAliases</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"mappings:"</span><span class="token operator">+</span>response<span class="token punctuation">.</span><span class="token function">getMappings</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"settings:"</span><span class="token operator">+</span>response<span class="token punctuation">.</span><span class="token function">getSettings</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        client<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印：</p>
<pre class="line-numbers language-none"><code class="language-none">aliases:&#123;user2&#x3D;[]&#125;
mappings:&#123;user2&#x3D;org.elasticsearch.cluster.metadata.MappingMetadata@ad700514&#125;
settings:&#123;user2&#x3D;&#123;&quot;index.creation_date&quot;:&quot;1617948726976&quot;,&quot;index.number_of_replicas&quot;:&quot;1&quot;,&quot;index.number_of_shards&quot;:&quot;1&quot;,&quot;index.provided_name&quot;:&quot;user2&quot;,&quot;index.uuid&quot;:&quot;UGZ1ntcySnK6hWyP2qoVpQ&quot;,&quot;index.version.created&quot;:&quot;7080099&quot;&#125;&#125;

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="删除-1"><a href="#删除-1" class="headerlink" title="删除"></a>删除</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpHost</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>admin<span class="token punctuation">.</span>indices<span class="token punctuation">.</span>delete<span class="token punctuation">.</span></span><span class="token class-name">DeleteIndexRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>support<span class="token punctuation">.</span>master<span class="token punctuation">.</span></span><span class="token class-name">AcknowledgedResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestClient</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestHighLevelClient</span></span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DeleteIndex</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">RestHighLevelClient</span> client <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
                <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">"localhost"</span><span class="token punctuation">,</span> <span class="token number">9200</span><span class="token punctuation">,</span> <span class="token string">"http"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 删除索引 - 请求对象</span>
        <span class="token class-name">DeleteIndexRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DeleteIndexRequest</span><span class="token punctuation">(</span><span class="token string">"user2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 发送请求，获取响应</span>
        <span class="token class-name">AcknowledgedResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 操作结果</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"操作结果 ： "</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">isAcknowledged</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        client<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印：</p>
<pre class="line-numbers language-none"><code class="language-none">操作结果：true

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h2 id="21-入门-JavaAPI-文档-新增-amp-修改"><a href="#21-入门-JavaAPI-文档-新增-amp-修改" class="headerlink" title="21-入门-JavaAPI-文档-新增 &amp; 修改"></a>21-入门-JavaAPI-文档-新增 &amp; 修改</h2><h3 id="重构"><a href="#重构" class="headerlink" title="重构"></a>重构</h3><p>上文由于频繁使用以下连接Elasticsearch和关闭它的代码，于是<strong>个人</strong>对它进行重构。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SomeClass</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">RestHighLevelClient</span> client <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
                <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">"localhost"</span><span class="token punctuation">,</span> <span class="token number">9200</span><span class="token punctuation">,</span> <span class="token string">"http"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		
        <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
        
        client<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>重构后的代码：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestHighLevelClient</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ElasticsearchTask</span> <span class="token punctuation">&#123;</span>

    <span class="token keyword">void</span> <span class="token function">doSomething</span><span class="token punctuation">(</span><span class="token class-name">RestHighLevelClient</span> client<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span><span class="token punctuation">;</span>

<span class="token punctuation">&#125;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">&#123;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">connect</span><span class="token punctuation">(</span><span class="token class-name">ElasticsearchTask</span> task<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">// 创建客户端对象</span>
        <span class="token class-name">RestHighLevelClient</span> client <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
                <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">"localhost"</span><span class="token punctuation">,</span> <span class="token number">9200</span><span class="token punctuation">,</span> <span class="token string">"http"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
            task<span class="token punctuation">.</span><span class="token function">doSomething</span><span class="token punctuation">(</span>client<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 关闭客户端连接</span>
            client<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>接下来，如果想让Elasticsearch完成一些操作，就编写一个lambda式即可。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SomeClass</span> <span class="token punctuation">&#123;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
			<span class="token comment">//do something</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>

<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="新增"><a href="#新增" class="headerlink" title="新增"></a>新增</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>fasterxml<span class="token punctuation">.</span>jackson<span class="token punctuation">.</span>databind<span class="token punctuation">.</span></span><span class="token class-name">ObjectMapper</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>model<span class="token punctuation">.</span></span><span class="token class-name">User</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>index<span class="token punctuation">.</span></span><span class="token class-name">IndexRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>index<span class="token punctuation">.</span></span><span class="token class-name">IndexResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>common<span class="token punctuation">.</span>xcontent<span class="token punctuation">.</span></span><span class="token class-name">XContentType</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">InsertDoc</span> <span class="token punctuation">&#123;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
            <span class="token comment">// 新增文档 - 请求对象</span>
            <span class="token class-name">IndexRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 设置索引及唯一性标识</span>
            request<span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1001"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token comment">// 创建数据对象</span>
            <span class="token class-name">User</span> user <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            user<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">"zhangsan"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            user<span class="token punctuation">.</span><span class="token function">setAge</span><span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            user<span class="token punctuation">.</span><span class="token function">setSex</span><span class="token punctuation">(</span><span class="token string">"男"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token class-name">ObjectMapper</span> objectMapper <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectMapper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">String</span> productJson <span class="token operator">=</span> objectMapper<span class="token punctuation">.</span><span class="token function">writeValueAsString</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 添加文档数据，数据格式为 JSON 格式</span>
            request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>productJson<span class="token punctuation">,</span> <span class="token class-name">XContentType</span><span class="token punctuation">.</span><span class="token constant">JSON</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 客户端发送请求，获取响应对象</span>
            <span class="token class-name">IndexResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token number">3.</span>打印结果信息
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"_index:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getIndex</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"_id:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"_result:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getResult</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印：</p>
<pre class="line-numbers language-none"><code class="language-none">_index:user
_id:1001
_result:UPDATED

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="修改"><a href="#修改" class="headerlink" title="修改"></a>修改</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>update<span class="token punctuation">.</span></span><span class="token class-name">UpdateRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>update<span class="token punctuation">.</span></span><span class="token class-name">UpdateResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>common<span class="token punctuation">.</span>xcontent<span class="token punctuation">.</span></span><span class="token class-name">XContentType</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UpdateDoc</span> <span class="token punctuation">&#123;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
            <span class="token comment">// 修改文档 - 请求对象</span>
            <span class="token class-name">UpdateRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">UpdateRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 配置修改参数</span>
            request<span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1001"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 设置请求体，对数据进行修改</span>
            request<span class="token punctuation">.</span><span class="token function">doc</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span><span class="token constant">JSON</span><span class="token punctuation">,</span> <span class="token string">"sex"</span><span class="token punctuation">,</span> <span class="token string">"女"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 客户端发送请求，获取响应对象</span>
            <span class="token class-name">UpdateResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"_index:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getIndex</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"_id:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"_result:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getResult</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印：</p>
<pre class="line-numbers language-none"><code class="language-none">_index:user
_id:1001
_result:UPDATED

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="22-入门-JavaAPI-文档-查询-amp-删除"><a href="#22-入门-JavaAPI-文档-查询-amp-删除" class="headerlink" title="22-入门-JavaAPI-文档-查询 &amp; 删除"></a>22-入门-JavaAPI-文档-查询 &amp; 删除</h2><h3 id="查询-1"><a href="#查询-1" class="headerlink" title="查询"></a>查询</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>get<span class="token punctuation">.</span></span><span class="token class-name">GetRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>get<span class="token punctuation">.</span></span><span class="token class-name">GetResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">GetDoc</span> <span class="token punctuation">&#123;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//1.创建请求对象</span>
            <span class="token class-name">GetRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">GetRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1001"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//2.客户端发送请求，获取响应对象</span>
            <span class="token class-name">GetResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token number">3.</span>打印结果信息
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"_index:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getIndex</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"_type:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"_id:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"source:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getSourceAsString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>

<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印：</p>
<pre class="line-numbers language-none"><code class="language-none">_index:user
_type:_doc
_id:1001
source:&#123;&quot;name&quot;:&quot;zhangsan&quot;,&quot;age&quot;:30,&quot;sex&quot;:&quot;男&quot;&#125;

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="删除-2"><a href="#删除-2" class="headerlink" title="删除"></a>删除</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>delete<span class="token punctuation">.</span></span><span class="token class-name">DeleteRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>delete<span class="token punctuation">.</span></span><span class="token class-name">DeleteResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DeleteDoc</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//创建请求对象</span>
            <span class="token class-name">DeleteRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DeleteRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1001"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//客户端发送请求，获取响应对象</span>
            <span class="token class-name">DeleteResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//打印信息</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印：</p>
<pre class="line-numbers language-none"><code class="language-none">DeleteResponse[index&#x3D;user,type&#x3D;_doc,id&#x3D;1001,version&#x3D;16,result&#x3D;deleted,shards&#x3D;ShardInfo&#123;total&#x3D;2, successful&#x3D;1, failures&#x3D;[]&#125;]

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h2 id="23-入门-JavaAPI-文档-批量新增-amp-批量删除"><a href="#23-入门-JavaAPI-文档-批量新增-amp-批量删除" class="headerlink" title="23-入门-JavaAPI-文档-批量新增 &amp; 批量删除"></a>23-入门-JavaAPI-文档-批量新增 &amp; 批量删除</h2><h3 id="批量新增"><a href="#批量新增" class="headerlink" title="批量新增"></a>批量新增</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>bulk<span class="token punctuation">.</span></span><span class="token class-name">BulkRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>bulk<span class="token punctuation">.</span></span><span class="token class-name">BulkResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>index<span class="token punctuation">.</span></span><span class="token class-name">IndexRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>common<span class="token punctuation">.</span>xcontent<span class="token punctuation">.</span></span><span class="token class-name">XContentType</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BatchInsertDoc</span> <span class="token punctuation">&#123;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//创建批量新增请求对象</span>
            <span class="token class-name">BulkRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BulkRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span>
                    <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1001"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span><span class="token constant">JSON</span><span class="token punctuation">,</span> <span class="token string">"name"</span><span class="token punctuation">,</span>
                    <span class="token string">"zhangsan"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span>
                    <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1002"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span><span class="token constant">JSON</span><span class="token punctuation">,</span> <span class="token string">"name"</span><span class="token punctuation">,</span>
                            <span class="token string">"lisi"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span>
                    <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1003"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span><span class="token constant">JSON</span><span class="token punctuation">,</span> <span class="token string">"name"</span><span class="token punctuation">,</span>
                    <span class="token string">"wangwu"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//客户端发送请求，获取响应对象</span>
            <span class="token class-name">BulkResponse</span> responses <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">bulk</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//打印结果信息</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"took:"</span> <span class="token operator">+</span> responses<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"items:"</span> <span class="token operator">+</span> responses<span class="token punctuation">.</span><span class="token function">getItems</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印</p>
<pre class="line-numbers language-none"><code class="language-none">took:294ms
items:[Lorg.elasticsearch.action.bulk.BulkItemResponse;@2beee7ff

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="批量删除"><a href="#批量删除" class="headerlink" title="批量删除"></a>批量删除</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>bulk<span class="token punctuation">.</span></span><span class="token class-name">BulkRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>bulk<span class="token punctuation">.</span></span><span class="token class-name">BulkResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>delete<span class="token punctuation">.</span></span><span class="token class-name">DeleteRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BatchDeleteDoc</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//创建批量删除请求对象</span>
            <span class="token class-name">BulkRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BulkRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DeleteRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1001"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DeleteRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1002"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DeleteRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1003"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//客户端发送请求，获取响应对象</span>
            <span class="token class-name">BulkResponse</span> responses <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">bulk</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//打印结果信息</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"took:"</span> <span class="token operator">+</span> responses<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"items:"</span> <span class="token operator">+</span> responses<span class="token punctuation">.</span><span class="token function">getItems</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印</p>
<pre class="line-numbers language-none"><code class="language-none">took:108ms
items:[Lorg.elasticsearch.action.bulk.BulkItemResponse;@7b02881e

Process finished with exit code 0

<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="24-入门-JavaAPI-文档-高级查询-全量查询"><a href="#24-入门-JavaAPI-文档-高级查询-全量查询" class="headerlink" title="24-入门-JavaAPI-文档-高级查询-全量查询"></a>24-入门-JavaAPI-文档-高级查询-全量查询</h2><p>先批量增加数据</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BatchInsertDoc</span> <span class="token punctuation">&#123;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//创建批量新增请求对象</span>
            <span class="token class-name">BulkRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BulkRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1001"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span><span class="token constant">JSON</span><span class="token punctuation">,</span> <span class="token string">"name"</span><span class="token punctuation">,</span> <span class="token string">"zhangsan"</span><span class="token punctuation">,</span> <span class="token string">"age"</span><span class="token punctuation">,</span> <span class="token string">"10"</span><span class="token punctuation">,</span> <span class="token string">"sex"</span><span class="token punctuation">,</span><span class="token string">"女"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1002"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span><span class="token constant">JSON</span><span class="token punctuation">,</span> <span class="token string">"name"</span><span class="token punctuation">,</span> <span class="token string">"lisi"</span><span class="token punctuation">,</span> <span class="token string">"age"</span><span class="token punctuation">,</span> <span class="token string">"30"</span><span class="token punctuation">,</span> <span class="token string">"sex"</span><span class="token punctuation">,</span><span class="token string">"女"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1003"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span><span class="token constant">JSON</span><span class="token punctuation">,</span> <span class="token string">"name"</span><span class="token punctuation">,</span> <span class="token string">"wangwu1"</span><span class="token punctuation">,</span> <span class="token string">"age"</span><span class="token punctuation">,</span> <span class="token string">"40"</span><span class="token punctuation">,</span> <span class="token string">"sex"</span><span class="token punctuation">,</span><span class="token string">"男"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1004"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span><span class="token constant">JSON</span><span class="token punctuation">,</span> <span class="token string">"name"</span><span class="token punctuation">,</span> <span class="token string">"wangwu2"</span><span class="token punctuation">,</span> <span class="token string">"age"</span><span class="token punctuation">,</span> <span class="token string">"20"</span><span class="token punctuation">,</span> <span class="token string">"sex"</span><span class="token punctuation">,</span><span class="token string">"女"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1005"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span><span class="token constant">JSON</span><span class="token punctuation">,</span> <span class="token string">"name"</span><span class="token punctuation">,</span> <span class="token string">"wangwu3"</span><span class="token punctuation">,</span> <span class="token string">"age"</span><span class="token punctuation">,</span> <span class="token string">"50"</span><span class="token punctuation">,</span> <span class="token string">"sex"</span><span class="token punctuation">,</span><span class="token string">"男"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">"1006"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span><span class="token constant">JSON</span><span class="token punctuation">,</span> <span class="token string">"name"</span><span class="token punctuation">,</span> <span class="token string">"wangwu4"</span><span class="token punctuation">,</span> <span class="token string">"age"</span><span class="token punctuation">,</span> <span class="token string">"20"</span><span class="token punctuation">,</span> <span class="token string">"sex"</span><span class="token punctuation">,</span><span class="token string">"男"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//客户端发送请求，获取响应对象</span>
            <span class="token class-name">BulkResponse</span> responses <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">bulk</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//打印结果信息</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"took:"</span> <span class="token operator">+</span> responses<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"items:"</span> <span class="token operator">+</span> responses<span class="token punctuation">.</span><span class="token function">getItems</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印</p>
<pre class="line-numbers language-none"><code class="language-none">took:168ms
items:[Lorg.elasticsearch.action.bulk.BulkItemResponse;@2beee7ff

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p><strong>查询所有索引数据</strong></p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">QueryBuilders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHit</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHits</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>builder<span class="token punctuation">.</span></span><span class="token class-name">SearchSourceBuilder</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">QueryDoc</span> <span class="token punctuation">&#123;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
            <span class="token comment">// 创建搜索请求对象</span>
            <span class="token class-name">SearchRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 构建查询的请求体</span>
            <span class="token class-name">SearchSourceBuilder</span> sourceBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 查询所有数据</span>
            sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchAllQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>sourceBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">SearchResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 查询匹配</span>
            <span class="token class-name">SearchHits</span> hits <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"took:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"timeout:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">isTimedOut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"total:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getTotalHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"MaxScore:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getMaxScore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hits========>>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">SearchHit</span> hit <span class="token operator">:</span> hits<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//输出每条查询的结果信息</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>hit<span class="token punctuation">.</span><span class="token function">getSourceAsString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"&lt;&lt;========"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印</p>
<pre class="line-numbers language-none"><code class="language-none">took:2ms
timeout:false
total:6 hits
MaxScore:1.0
hits&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;
&#123;&quot;name&quot;:&quot;zhangsan&quot;,&quot;age&quot;:&quot;10&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&#123;&quot;name&quot;:&quot;lisi&quot;,&quot;age&quot;:&quot;30&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu1&quot;,&quot;age&quot;:&quot;40&quot;,&quot;sex&quot;:&quot;男&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu2&quot;,&quot;age&quot;:&quot;20&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu3&quot;,&quot;age&quot;:&quot;50&quot;,&quot;sex&quot;:&quot;男&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu4&quot;,&quot;age&quot;:&quot;20&quot;,&quot;sex&quot;:&quot;男&quot;&#125;
&lt;&lt;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="25-入门-JavaAPI-文档-高级查询-分页查询-amp-条件查询-amp-查询排序"><a href="#25-入门-JavaAPI-文档-高级查询-分页查询-amp-条件查询-amp-查询排序" class="headerlink" title="25-入门-JavaAPI-文档-高级查询-分页查询 &amp; 条件查询 &amp; 查询排序"></a>25-入门-JavaAPI-文档-高级查询-分页查询 &amp; 条件查询 &amp; 查询排序</h2><h3 id="条件查询-1"><a href="#条件查询-1" class="headerlink" title="条件查询"></a>条件查询</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ElasticsearchTask</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">QueryBuilders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHit</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHits</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>builder<span class="token punctuation">.</span></span><span class="token class-name">SearchSourceBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>sort<span class="token punctuation">.</span></span><span class="token class-name">SortOrder</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">QueryDoc</span> <span class="token punctuation">&#123;</span>
    
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">ElasticsearchTask</span> <span class="token constant">SEARCH_BY_CONDITION</span> <span class="token operator">=</span> client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 创建搜索请求对象</span>
        <span class="token class-name">SearchRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        request<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 构建查询的请求体</span>
        <span class="token class-name">SearchSourceBuilder</span> sourceBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">termQuery</span><span class="token punctuation">(</span><span class="token string">"age"</span><span class="token punctuation">,</span> <span class="token string">"30"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>sourceBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">SearchResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 查询匹配</span>
        <span class="token class-name">SearchHits</span> hits <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"took:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"timeout:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">isTimedOut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"total:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getTotalHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"MaxScore:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getMaxScore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hits========>>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">SearchHit</span> hit <span class="token operator">:</span> hits<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//输出每条查询的结果信息</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>hit<span class="token punctuation">.</span><span class="token function">getSourceAsString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"&lt;&lt;========"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token constant">SEARCH_BY_CONDITION</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印</p>
<pre class="line-numbers language-none"><code class="language-none">took:1ms
timeout:false
total:1 hits
MaxScore:1.0
hits&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;
&#123;&quot;name&quot;:&quot;lisi&quot;,&quot;age&quot;:&quot;30&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&lt;&lt;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="分页查询-1"><a href="#分页查询-1" class="headerlink" title="分页查询"></a>分页查询</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ElasticsearchTask</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">QueryBuilders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHit</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHits</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>builder<span class="token punctuation">.</span></span><span class="token class-name">SearchSourceBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>sort<span class="token punctuation">.</span></span><span class="token class-name">SortOrder</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">QueryDoc</span> <span class="token punctuation">&#123;</span>
    
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">ElasticsearchTask</span> <span class="token constant">SEARCH_BY_PAGING</span> <span class="token operator">=</span> client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 创建搜索请求对象</span>
        <span class="token class-name">SearchRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        request<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 构建查询的请求体</span>
        <span class="token class-name">SearchSourceBuilder</span> sourceBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchAllQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 分页查询</span>
        <span class="token comment">// 当前页其实索引(第一条数据的顺序号)， from</span>
        sourceBuilder<span class="token punctuation">.</span><span class="token function">from</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 每页显示多少条 size</span>
        sourceBuilder<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>sourceBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">SearchResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 查询匹配</span>
        <span class="token class-name">SearchHits</span> hits <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"took:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"timeout:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">isTimedOut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"total:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getTotalHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"MaxScore:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getMaxScore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hits========>>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">SearchHit</span> hit <span class="token operator">:</span> hits<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//输出每条查询的结果信息</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>hit<span class="token punctuation">.</span><span class="token function">getSourceAsString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"&lt;&lt;========"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token constant">SEARCH_BY_CONDITION</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印</p>
<pre class="line-numbers language-none"><code class="language-none">took:1ms
timeout:false
total:6 hits
MaxScore:1.0
hits&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;
&#123;&quot;name&quot;:&quot;zhangsan&quot;,&quot;age&quot;:&quot;10&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&#123;&quot;name&quot;:&quot;lisi&quot;,&quot;age&quot;:&quot;30&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&lt;&lt;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="查询排序-1"><a href="#查询排序-1" class="headerlink" title="查询排序"></a>查询排序</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ElasticsearchTask</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">QueryBuilders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHit</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHits</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>builder<span class="token punctuation">.</span></span><span class="token class-name">SearchSourceBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>sort<span class="token punctuation">.</span></span><span class="token class-name">SortOrder</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">QueryDoc</span> <span class="token punctuation">&#123;</span>
    
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">ElasticsearchTask</span> <span class="token constant">SEARCH_WITH_ORDER</span> <span class="token operator">=</span> client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 创建搜索请求对象</span>
        <span class="token class-name">SearchRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        request<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 构建查询的请求体</span>
        <span class="token class-name">SearchSourceBuilder</span> sourceBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchAllQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 排序</span>
        sourceBuilder<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token string">"age"</span><span class="token punctuation">,</span> <span class="token class-name">SortOrder</span><span class="token punctuation">.</span><span class="token constant">ASC</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>sourceBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">SearchResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 查询匹配</span>
        <span class="token class-name">SearchHits</span> hits <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"took:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"timeout:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">isTimedOut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"total:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getTotalHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"MaxScore:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getMaxScore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hits========>>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">SearchHit</span> hit <span class="token operator">:</span> hits<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//输出每条查询的结果信息</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>hit<span class="token punctuation">.</span><span class="token function">getSourceAsString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"&lt;&lt;========"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token constant">SEARCH_WITH_ORDER</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span>

<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印</p>
<pre class="line-numbers language-none"><code class="language-none">took:1ms
timeout:false
total:6 hits
MaxScore:NaN
hits&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;
&#123;&quot;name&quot;:&quot;zhangsan&quot;,&quot;age&quot;:&quot;10&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu2&quot;,&quot;age&quot;:&quot;20&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu4&quot;,&quot;age&quot;:&quot;20&quot;,&quot;sex&quot;:&quot;男&quot;&#125;
&#123;&quot;name&quot;:&quot;lisi&quot;,&quot;age&quot;:&quot;30&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu1&quot;,&quot;age&quot;:&quot;40&quot;,&quot;sex&quot;:&quot;男&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu3&quot;,&quot;age&quot;:&quot;50&quot;,&quot;sex&quot;:&quot;男&quot;&#125;
&lt;&lt;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="26-入门-JavaAPI-文档-高级查询-组合查询-amp-范围查询"><a href="#26-入门-JavaAPI-文档-高级查询-组合查询-amp-范围查询" class="headerlink" title="26-入门-JavaAPI-文档-高级查询-组合查询 &amp; 范围查询"></a>26-入门-JavaAPI-文档-高级查询-组合查询 &amp; 范围查询</h2><h3 id="组合查询"><a href="#组合查询" class="headerlink" title="组合查询"></a>组合查询</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ElasticsearchTask</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">BoolQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">QueryBuilders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHit</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHits</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>builder<span class="token punctuation">.</span></span><span class="token class-name">SearchSourceBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>sort<span class="token punctuation">.</span></span><span class="token class-name">SortOrder</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">QueryDoc</span> <span class="token punctuation">&#123;</span>
    
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">ElasticsearchTask</span> <span class="token constant">SEARCH_BY_BOOL_CONDITION</span> <span class="token operator">=</span> client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 创建搜索请求对象</span>
        <span class="token class-name">SearchRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        request<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 构建查询的请求体</span>
        <span class="token class-name">SearchSourceBuilder</span> sourceBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">BoolQueryBuilder</span> boolQueryBuilder <span class="token operator">=</span> <span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">boolQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 必须包含</span>
        boolQueryBuilder<span class="token punctuation">.</span><span class="token function">must</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchQuery</span><span class="token punctuation">(</span><span class="token string">"age"</span><span class="token punctuation">,</span> <span class="token string">"30"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 一定不含</span>
        boolQueryBuilder<span class="token punctuation">.</span><span class="token function">mustNot</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchQuery</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">,</span> <span class="token string">"zhangsan"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 可能包含</span>
        boolQueryBuilder<span class="token punctuation">.</span><span class="token function">should</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchQuery</span><span class="token punctuation">(</span><span class="token string">"sex"</span><span class="token punctuation">,</span> <span class="token string">"男"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>boolQueryBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>sourceBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">SearchResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 查询匹配</span>
        <span class="token class-name">SearchHits</span> hits <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"took:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"timeout:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">isTimedOut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"total:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getTotalHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"MaxScore:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getMaxScore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hits========>>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">SearchHit</span> hit <span class="token operator">:</span> hits<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//输出每条查询的结果信息</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>hit<span class="token punctuation">.</span><span class="token function">getSourceAsString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"&lt;&lt;========"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token constant">SEARCH_BY_BOOL_CONDITION</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印</p>
<pre class="line-numbers language-none"><code class="language-none">took:28ms
timeout:false
total:1 hits
MaxScore:1.0
hits&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;
&#123;&quot;name&quot;:&quot;lisi&quot;,&quot;age&quot;:&quot;30&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&lt;&lt;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="范围查询-1"><a href="#范围查询-1" class="headerlink" title="范围查询"></a>范围查询</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ElasticsearchTask</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">BoolQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">QueryBuilders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">RangeQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHit</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHits</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>builder<span class="token punctuation">.</span></span><span class="token class-name">SearchSourceBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>sort<span class="token punctuation">.</span></span><span class="token class-name">SortOrder</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">QueryDoc</span> <span class="token punctuation">&#123;</span>
    
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">ElasticsearchTask</span> <span class="token constant">SEARCH_BY_RANGE</span> <span class="token operator">=</span> client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 创建搜索请求对象</span>
        <span class="token class-name">SearchRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        request<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 构建查询的请求体</span>
        <span class="token class-name">SearchSourceBuilder</span> sourceBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">RangeQueryBuilder</span> rangeQuery <span class="token operator">=</span> <span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">rangeQuery</span><span class="token punctuation">(</span><span class="token string">"age"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 大于等于</span>
        <span class="token comment">//rangeQuery.gte("30");</span>
        <span class="token comment">// 小于等于</span>
        rangeQuery<span class="token punctuation">.</span><span class="token function">lte</span><span class="token punctuation">(</span><span class="token string">"40"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>rangeQuery<span class="token punctuation">)</span><span class="token punctuation">;</span>
        request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>sourceBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">SearchResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 查询匹配</span>
        <span class="token class-name">SearchHits</span> hits <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"took:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"timeout:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">isTimedOut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"total:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getTotalHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"MaxScore:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getMaxScore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hits========>>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">SearchHit</span> hit <span class="token operator">:</span> hits<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//输出每条查询的结果信息</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>hit<span class="token punctuation">.</span><span class="token function">getSourceAsString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"&lt;&lt;========"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token constant">SEARCH_BY_RANGE</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span>

<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印</p>
<pre class="line-numbers language-none"><code class="language-none">took:1ms
timeout:false
total:5 hits
MaxScore:1.0
hits&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;
&#123;&quot;name&quot;:&quot;zhangsan&quot;,&quot;age&quot;:&quot;10&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&#123;&quot;name&quot;:&quot;lisi&quot;,&quot;age&quot;:&quot;30&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu1&quot;,&quot;age&quot;:&quot;40&quot;,&quot;sex&quot;:&quot;男&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu2&quot;,&quot;age&quot;:&quot;20&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu4&quot;,&quot;age&quot;:&quot;20&quot;,&quot;sex&quot;:&quot;男&quot;&#125;
&lt;&lt;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="27-入门-JavaAPI-文档-高级查询-模糊查询-amp-高亮查询"><a href="#27-入门-JavaAPI-文档-高级查询-模糊查询-amp-高亮查询" class="headerlink" title="27-入门-JavaAPI-文档-高级查询-模糊查询 &amp; 高亮查询"></a>27-入门-JavaAPI-文档-高级查询-模糊查询 &amp; 高亮查询</h2><h3 id="模糊查询"><a href="#模糊查询" class="headerlink" title="模糊查询"></a>模糊查询</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ElasticsearchTask</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>common<span class="token punctuation">.</span>unit<span class="token punctuation">.</span></span><span class="token class-name">Fuzziness</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">BoolQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">QueryBuilders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">RangeQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHit</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHits</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>builder<span class="token punctuation">.</span></span><span class="token class-name">SearchSourceBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>sort<span class="token punctuation">.</span></span><span class="token class-name">SortOrder</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">QueryDoc</span> <span class="token punctuation">&#123;</span>
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">ElasticsearchTask</span> <span class="token constant">SEARCH_BY_FUZZY_CONDITION</span> <span class="token operator">=</span> client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 创建搜索请求对象</span>
        <span class="token class-name">SearchRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        request<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 构建查询的请求体</span>
        <span class="token class-name">SearchSourceBuilder</span> sourceBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">fuzzyQuery</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">,</span><span class="token string">"wangwu"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">fuzziness</span><span class="token punctuation">(</span><span class="token class-name">Fuzziness</span><span class="token punctuation">.</span><span class="token constant">ONE</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>sourceBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">SearchResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 查询匹配</span>
        <span class="token class-name">SearchHits</span> hits <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"took:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"timeout:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">isTimedOut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"total:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getTotalHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"MaxScore:"</span> <span class="token operator">+</span> hits<span class="token punctuation">.</span><span class="token function">getMaxScore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hits========>>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">SearchHit</span> hit <span class="token operator">:</span> hits<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//输出每条查询的结果信息</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>hit<span class="token punctuation">.</span><span class="token function">getSourceAsString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"&lt;&lt;========"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>


    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
<span class="token comment">//        ConnectElasticsearch.connect(SEARCH_ALL);</span>
<span class="token comment">//        ConnectElasticsearch.connect(SEARCH_BY_CONDITION);</span>
<span class="token comment">//        ConnectElasticsearch.connect(SEARCH_BY_PAGING);</span>
<span class="token comment">//        ConnectElasticsearch.connect(SEARCH_WITH_ORDER);</span>
<span class="token comment">//        ConnectElasticsearch.connect(SEARCH_BY_BOOL_CONDITION);</span>
<span class="token comment">//        ConnectElasticsearch.connect(SEARCH_BY_RANGE);</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token constant">SEARCH_BY_FUZZY_CONDITION</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印</p>
<pre class="line-numbers language-none"><code class="language-none">took:152ms
timeout:false
total:4 hits
MaxScore:1.2837042
hits&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;
&#123;&quot;name&quot;:&quot;wangwu1&quot;,&quot;age&quot;:&quot;40&quot;,&quot;sex&quot;:&quot;男&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu2&quot;,&quot;age&quot;:&quot;20&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu3&quot;,&quot;age&quot;:&quot;50&quot;,&quot;sex&quot;:&quot;男&quot;&#125;
&#123;&quot;name&quot;:&quot;wangwu4&quot;,&quot;age&quot;:&quot;20&quot;,&quot;sex&quot;:&quot;男&quot;&#125;
&lt;&lt;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="高亮查询-1"><a href="#高亮查询-1" class="headerlink" title="高亮查询"></a>高亮查询</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ElasticsearchTask</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>common<span class="token punctuation">.</span>unit<span class="token punctuation">.</span></span><span class="token class-name">Fuzziness</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">BoolQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">QueryBuilders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">RangeQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">TermsQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHit</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHits</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>builder<span class="token punctuation">.</span></span><span class="token class-name">SearchSourceBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>fetch<span class="token punctuation">.</span>subphase<span class="token punctuation">.</span>highlight<span class="token punctuation">.</span></span><span class="token class-name">HighlightBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>fetch<span class="token punctuation">.</span>subphase<span class="token punctuation">.</span>highlight<span class="token punctuation">.</span></span><span class="token class-name">HighlightField</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>sort<span class="token punctuation">.</span></span><span class="token class-name">SortOrder</span></span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Map</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">QueryDoc</span> <span class="token punctuation">&#123;</span>
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">ElasticsearchTask</span> <span class="token constant">SEARCH_WITH_HIGHLIGHT</span> <span class="token operator">=</span> client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 高亮查询</span>
        <span class="token class-name">SearchRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//2.创建查询请求体构建器</span>
        <span class="token class-name">SearchSourceBuilder</span> sourceBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//构建查询方式：高亮查询</span>
        <span class="token class-name">TermsQueryBuilder</span> termsQueryBuilder <span class="token operator">=</span>
                <span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">termsQuery</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">,</span><span class="token string">"zhangsan"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//设置查询方式</span>
        sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>termsQueryBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//构建高亮字段</span>
        <span class="token class-name">HighlightBuilder</span> highlightBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HighlightBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        highlightBuilder<span class="token punctuation">.</span><span class="token function">preTags</span><span class="token punctuation">(</span><span class="token string">"&lt;font color='red'>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//设置标签前缀</span>
        highlightBuilder<span class="token punctuation">.</span><span class="token function">postTags</span><span class="token punctuation">(</span><span class="token string">"&lt;/font>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//设置标签后缀</span>
        highlightBuilder<span class="token punctuation">.</span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//设置高亮字段</span>
        <span class="token comment">//设置高亮构建对象</span>
        sourceBuilder<span class="token punctuation">.</span><span class="token function">highlighter</span><span class="token punctuation">(</span>highlightBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//设置请求体</span>
        request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>sourceBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//3.客户端发送请求，获取响应对象</span>
        <span class="token class-name">SearchResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//4.打印响应结果</span>
        <span class="token class-name">SearchHits</span> hits <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"took::"</span><span class="token operator">+</span>response<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"time_out::"</span><span class="token operator">+</span>response<span class="token punctuation">.</span><span class="token function">isTimedOut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"total::"</span><span class="token operator">+</span>hits<span class="token punctuation">.</span><span class="token function">getTotalHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"max_score::"</span><span class="token operator">+</span>hits<span class="token punctuation">.</span><span class="token function">getMaxScore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hits::::>>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">SearchHit</span> hit <span class="token operator">:</span> hits<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token class-name">String</span> sourceAsString <span class="token operator">=</span> hit<span class="token punctuation">.</span><span class="token function">getSourceAsString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>sourceAsString<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//打印高亮结果</span>
            <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">HighlightField</span><span class="token punctuation">></span></span> highlightFields <span class="token operator">=</span> hit<span class="token punctuation">.</span><span class="token function">getHighlightFields</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>highlightFields<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"&lt;&lt;::::"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>


    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token constant">SEARCH_WITH_HIGHLIGHT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印</p>
<pre class="line-numbers language-none"><code class="language-none">took::672ms
time_out::false
total::1 hits
max_score::1.0
hits::::&gt;&gt;
&#123;&quot;name&quot;:&quot;zhangsan&quot;,&quot;age&quot;:&quot;10&quot;,&quot;sex&quot;:&quot;女&quot;&#125;
&#123;name&#x3D;[name], fragments[[&lt;font color&#x3D;&#39;red&#39;&gt;zhangsan&lt;&#x2F;font&gt;]]&#125;
&lt;&lt;::::

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="28-入门-JavaAPI-文档-高级查询-最大值查询-amp-分组查询"><a href="#28-入门-JavaAPI-文档-高级查询-最大值查询-amp-分组查询" class="headerlink" title="28-入门-JavaAPI-文档-高级查询-最大值查询 &amp; 分组查询"></a>28-入门-JavaAPI-文档-高级查询-最大值查询 &amp; 分组查询</h2><h3 id="最大值查询"><a href="#最大值查询" class="headerlink" title="最大值查询"></a>最大值查询</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ElasticsearchTask</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>common<span class="token punctuation">.</span>unit<span class="token punctuation">.</span></span><span class="token class-name">Fuzziness</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">BoolQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">QueryBuilders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">RangeQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">TermsQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHit</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHits</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>aggregations<span class="token punctuation">.</span></span><span class="token class-name">AggregationBuilders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>builder<span class="token punctuation">.</span></span><span class="token class-name">SearchSourceBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>fetch<span class="token punctuation">.</span>subphase<span class="token punctuation">.</span>highlight<span class="token punctuation">.</span></span><span class="token class-name">HighlightBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>fetch<span class="token punctuation">.</span>subphase<span class="token punctuation">.</span>highlight<span class="token punctuation">.</span></span><span class="token class-name">HighlightField</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>sort<span class="token punctuation">.</span></span><span class="token class-name">SortOrder</span></span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Map</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">QueryDoc</span> <span class="token punctuation">&#123;</span>
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">ElasticsearchTask</span> <span class="token constant">SEARCH_WITH_MAX</span> <span class="token operator">=</span> client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 高亮查询</span>
        <span class="token class-name">SearchRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">SearchSourceBuilder</span> sourceBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sourceBuilder<span class="token punctuation">.</span><span class="token function">aggregation</span><span class="token punctuation">(</span><span class="token class-name">AggregationBuilders</span><span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token string">"maxAge"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string">"age"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//设置请求体</span>
        request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>sourceBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//3.客户端发送请求，获取响应对象</span>
        <span class="token class-name">SearchResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//4.打印响应结果</span>
        <span class="token class-name">SearchHits</span> hits <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>response<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token constant">SEARCH_WITH_MAX</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印</p>
<pre class="line-numbers language-none"><code class="language-none">&#123;&quot;took&quot;:16,&quot;timed_out&quot;:false,&quot;_shards&quot;:&#123;&quot;total&quot;:1,&quot;successful&quot;:1,&quot;skipped&quot;:0,&quot;failed&quot;:0&#125;,&quot;hits&quot;:&#123;&quot;total&quot;:&#123;&quot;value&quot;:6,&quot;relation&quot;:&quot;eq&quot;&#125;,&quot;max_score&quot;:1.0,&quot;hits&quot;:[&#123;&quot;_index&quot;:&quot;user&quot;,&quot;_type&quot;:&quot;_doc&quot;,&quot;_id&quot;:&quot;1001&quot;,&quot;_score&quot;:1.0,&quot;_source&quot;:&#123;&quot;name&quot;:&quot;zhangsan&quot;,&quot;age&quot;:&quot;10&quot;,&quot;sex&quot;:&quot;女&quot;&#125;&#125;,&#123;&quot;_index&quot;:&quot;user&quot;,&quot;_type&quot;:&quot;_doc&quot;,&quot;_id&quot;:&quot;1002&quot;,&quot;_score&quot;:1.0,&quot;_source&quot;:&#123;&quot;name&quot;:&quot;lisi&quot;,&quot;age&quot;:&quot;30&quot;,&quot;sex&quot;:&quot;女&quot;&#125;&#125;,&#123;&quot;_index&quot;:&quot;user&quot;,&quot;_type&quot;:&quot;_doc&quot;,&quot;_id&quot;:&quot;1003&quot;,&quot;_score&quot;:1.0,&quot;_source&quot;:&#123;&quot;name&quot;:&quot;wangwu1&quot;,&quot;age&quot;:&quot;40&quot;,&quot;sex&quot;:&quot;男&quot;&#125;&#125;,&#123;&quot;_index&quot;:&quot;user&quot;,&quot;_type&quot;:&quot;_doc&quot;,&quot;_id&quot;:&quot;1004&quot;,&quot;_score&quot;:1.0,&quot;_source&quot;:&#123;&quot;name&quot;:&quot;wangwu2&quot;,&quot;age&quot;:&quot;20&quot;,&quot;sex&quot;:&quot;女&quot;&#125;&#125;,&#123;&quot;_index&quot;:&quot;user&quot;,&quot;_type&quot;:&quot;_doc&quot;,&quot;_id&quot;:&quot;1005&quot;,&quot;_score&quot;:1.0,&quot;_source&quot;:&#123;&quot;name&quot;:&quot;wangwu3&quot;,&quot;age&quot;:&quot;50&quot;,&quot;sex&quot;:&quot;男&quot;&#125;&#125;,&#123;&quot;_index&quot;:&quot;user&quot;,&quot;_type&quot;:&quot;_doc&quot;,&quot;_id&quot;:&quot;1006&quot;,&quot;_score&quot;:1.0,&quot;_source&quot;:&#123;&quot;name&quot;:&quot;wangwu4&quot;,&quot;age&quot;:&quot;20&quot;,&quot;sex&quot;:&quot;男&quot;&#125;&#125;]&#125;,&quot;aggregations&quot;:&#123;&quot;max#maxAge&quot;:&#123;&quot;value&quot;:50.0&#125;&#125;&#125;

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h3 id="分组查询"><a href="#分组查询" class="headerlink" title="分组查询"></a>分组查询</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ConnectElasticsearch</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>hello<span class="token punctuation">.</span></span><span class="token class-name">ElasticsearchTask</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RequestOptions</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>common<span class="token punctuation">.</span>unit<span class="token punctuation">.</span></span><span class="token class-name">Fuzziness</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">BoolQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">QueryBuilders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">RangeQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">TermsQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHit</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span></span><span class="token class-name">SearchHits</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>aggregations<span class="token punctuation">.</span></span><span class="token class-name">AggregationBuilders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>builder<span class="token punctuation">.</span></span><span class="token class-name">SearchSourceBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>fetch<span class="token punctuation">.</span>subphase<span class="token punctuation">.</span>highlight<span class="token punctuation">.</span></span><span class="token class-name">HighlightBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>fetch<span class="token punctuation">.</span>subphase<span class="token punctuation">.</span>highlight<span class="token punctuation">.</span></span><span class="token class-name">HighlightField</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>search<span class="token punctuation">.</span>sort<span class="token punctuation">.</span></span><span class="token class-name">SortOrder</span></span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Map</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">QueryDoc</span> <span class="token punctuation">&#123;</span>

	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">ElasticsearchTask</span> <span class="token constant">SEARCH_WITH_GROUP</span> <span class="token operator">=</span> client <span class="token operator">-></span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">SearchRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">SearchSourceBuilder</span> sourceBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sourceBuilder<span class="token punctuation">.</span><span class="token function">aggregation</span><span class="token punctuation">(</span><span class="token class-name">AggregationBuilders</span><span class="token punctuation">.</span><span class="token function">terms</span><span class="token punctuation">(</span><span class="token string">"age_groupby"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string">"age"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//设置请求体</span>
        request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>sourceBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//3.客户端发送请求，获取响应对象</span>
        <span class="token class-name">SearchResponse</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//4.打印响应结果</span>
        <span class="token class-name">SearchHits</span> hits <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>response<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConnectElasticsearch</span><span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token constant">SEARCH_WITH_GROUP</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>后台打印</p>
<pre class="line-numbers language-none"><code class="language-none">&#123;&quot;took&quot;:10,&quot;timed_out&quot;:false,&quot;_shards&quot;:&#123;&quot;total&quot;:1,&quot;successful&quot;:1,&quot;skipped&quot;:0,&quot;failed&quot;:0&#125;,&quot;hits&quot;:&#123;&quot;total&quot;:&#123;&quot;value&quot;:6,&quot;relation&quot;:&quot;eq&quot;&#125;,&quot;max_score&quot;:1.0,&quot;hits&quot;:[&#123;&quot;_index&quot;:&quot;user&quot;,&quot;_type&quot;:&quot;_doc&quot;,&quot;_id&quot;:&quot;1001&quot;,&quot;_score&quot;:1.0,&quot;_source&quot;:&#123;&quot;name&quot;:&quot;zhangsan&quot;,&quot;age&quot;:&quot;10&quot;,&quot;sex&quot;:&quot;女&quot;&#125;&#125;,&#123;&quot;_index&quot;:&quot;user&quot;,&quot;_type&quot;:&quot;_doc&quot;,&quot;_id&quot;:&quot;1002&quot;,&quot;_score&quot;:1.0,&quot;_source&quot;:&#123;&quot;name&quot;:&quot;lisi&quot;,&quot;age&quot;:&quot;30&quot;,&quot;sex&quot;:&quot;女&quot;&#125;&#125;,&#123;&quot;_index&quot;:&quot;user&quot;,&quot;_type&quot;:&quot;_doc&quot;,&quot;_id&quot;:&quot;1003&quot;,&quot;_score&quot;:1.0,&quot;_source&quot;:&#123;&quot;name&quot;:&quot;wangwu1&quot;,&quot;age&quot;:&quot;40&quot;,&quot;sex&quot;:&quot;男&quot;&#125;&#125;,&#123;&quot;_index&quot;:&quot;user&quot;,&quot;_type&quot;:&quot;_doc&quot;,&quot;_id&quot;:&quot;1004&quot;,&quot;_score&quot;:1.0,&quot;_source&quot;:&#123;&quot;name&quot;:&quot;wangwu2&quot;,&quot;age&quot;:&quot;20&quot;,&quot;sex&quot;:&quot;女&quot;&#125;&#125;,&#123;&quot;_index&quot;:&quot;user&quot;,&quot;_type&quot;:&quot;_doc&quot;,&quot;_id&quot;:&quot;1005&quot;,&quot;_score&quot;:1.0,&quot;_source&quot;:&#123;&quot;name&quot;:&quot;wangwu3&quot;,&quot;age&quot;:&quot;50&quot;,&quot;sex&quot;:&quot;男&quot;&#125;&#125;,&#123;&quot;_index&quot;:&quot;user&quot;,&quot;_type&quot;:&quot;_doc&quot;,&quot;_id&quot;:&quot;1006&quot;,&quot;_score&quot;:1.0,&quot;_source&quot;:&#123;&quot;name&quot;:&quot;wangwu4&quot;,&quot;age&quot;:&quot;20&quot;,&quot;sex&quot;:&quot;男&quot;&#125;&#125;]&#125;,&quot;aggregations&quot;:&#123;&quot;lterms#age_groupby&quot;:&#123;&quot;doc_count_error_upper_bound&quot;:0,&quot;sum_other_doc_count&quot;:0,&quot;buckets&quot;:[&#123;&quot;key&quot;:20,&quot;doc_count&quot;:2&#125;,&#123;&quot;key&quot;:10,&quot;doc_count&quot;:1&#125;,&#123;&quot;key&quot;:30,&quot;doc_count&quot;:1&#125;,&#123;&quot;key&quot;:40,&quot;doc_count&quot;:1&#125;,&#123;&quot;key&quot;:50,&quot;doc_count&quot;:1&#125;]&#125;&#125;&#125;

Process finished with exit code 0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h1 id="第3章-Elasticsearch环境"><a href="#第3章-Elasticsearch环境" class="headerlink" title="第3章 Elasticsearch环境"></a>第3章 Elasticsearch环境</h1><h2 id="29-环境-简介"><a href="#29-环境-简介" class="headerlink" title="29-环境-简介"></a>29-环境-简介</h2><h3 id="单机-amp-集群"><a href="#单机-amp-集群" class="headerlink" title="单机 &amp; 集群"></a>单机 &amp; 集群</h3><p>单台Elasticsearch服务器提供服务，往往都有最大的负载能力，超过这个阈值，服务器<br>性能就会大大降低甚至不可用，所以生产环境中，一般都是运行在指定服务器集群中。<br>除了负载能力，单点服务器也存在其他问题：</p>
<ul>
<li>单台机器存储容量有限</li>
<li>单服务器容易出现单点故障，无法实现高可用</li>
<li>单服务的并发处理能力有限</li>
</ul>
<p>配置服务器集群时，集群中节点数量没有限制，大于等于2个节点就可以看做是集群了。一<br>般出于高性能及高可用方面来考虑集群中节点数量都是3个以上</p>
<p>总之，集群能提高性能，增加容错。</p>
<h3 id="集群-Cluster"><a href="#集群-Cluster" class="headerlink" title="集群 Cluster"></a>集群 Cluster</h3><p><strong>一个集群就是由一个或多个服务器节点组织在一起，共同持有整个的数据，并一起提供索引和搜索功能</strong>。一个Elasticsearch集群有一个唯一的名字标识，这个名字默认就是”elasticsearch”。这个名字是重要的，因为一个节点只能通过指定某个集群的名字，来加入这个集群。</p>
<h3 id="节点Node"><a href="#节点Node" class="headerlink" title="节点Node"></a>节点Node</h3><p>集群中包含很多服务器，一个节点就是其中的一个服务器。作为集群的一部分，它存储数据，参与集群的索引和搜索功能。</p>
<p>一个节点也是由一个名字来标识的，默认情况下，这个名字是一个随机的漫威漫画角色的名字，这个名字会在启动的时候赋予节点。这个名字对于管理工作来说挺重要的，因为在这个管理过程中，你会去确定网络中的哪些服务器对应于Elasticsearch集群中的哪些节点。</p>
<p>一个节点可以通过配置集群名称的方式来加入一个指定的集群。默认情况下，每个节点都会被安排加入到一个叫做“elasticsearch”的集群中，这意味着，如果你在你的网络中启动了若干个节点，并假定它们能够相互发现彼此，它们将会自动地形成并加入到一个叫做“elasticsearch”的集群中。</p>
<p>在一个集群里，只要你想，可以拥有任意多个节点。而且，如果当前你的网络中没有运<br>行任何Elasticsearch节点，这时启动一个节点，会默认创建并加入一个叫做“elasticsearch”的<br>集群。</p>
<h2 id="30-环境-Windows集群部署"><a href="#30-环境-Windows集群部署" class="headerlink" title="30-环境-Windows集群部署"></a>30-环境-Windows集群部署</h2><h3 id="部署集群"><a href="#部署集群" class="headerlink" title="部署集群"></a>部署集群</h3><p>一、创建elasticsearch-cluster文件夹</p>
<p>创建elasticsearch-7.8.0-cluster文件夹，在内部复制三个elasticsearch服务。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/6d6022b6d30a9e2c1a4e18092d5f130f.png" alt="img"></p>
<p>二、修改集群文件目录中每个节点的config&#x2F;elasticsearch.yml配置文件</p>
<p><strong>node-1001节点</strong></p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token comment">#节点1的配置信息：</span>
<span class="token comment">#集群名称，节点之间要保持一致</span>
<span class="token key atrule">cluster.name</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>elasticsearch
<span class="token comment">#节点名称，集群内要唯一</span>
<span class="token key atrule">node.name</span><span class="token punctuation">:</span> node<span class="token punctuation">-</span><span class="token number">1001</span>
<span class="token key atrule">node.master</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token key atrule">node.data</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token comment">#ip地址</span>
<span class="token key atrule">network.host</span><span class="token punctuation">:</span> localhost
<span class="token comment">#http端口</span>
<span class="token key atrule">http.port</span><span class="token punctuation">:</span> <span class="token number">1001</span>
<span class="token comment">#tcp监听端口</span>
<span class="token key atrule">transport.tcp.port</span><span class="token punctuation">:</span> <span class="token number">9301</span>
<span class="token comment">#discovery.seed_hosts: ["localhost:9301", "localhost:9302","localhost:9303"]</span>
<span class="token comment">#discovery.zen.fd.ping_timeout: 1m</span>
<span class="token comment">#discovery.zen.fd.ping_retries: 5</span>
<span class="token comment">#集群内的可以被选为主节点的节点列表</span>
<span class="token comment">#cluster.initial_master_nodes: ["node-1", "node-2","node-3"]</span>
<span class="token comment">#跨域配置</span>
<span class="token comment">#action.destructive_requires_name: true</span>
<span class="token key atrule">http.cors.enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token key atrule">http.cors.allow-origin</span><span class="token punctuation">:</span> <span class="token string">"*"</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><strong>node-1002节点</strong></p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token comment">#节点2的配置信息：</span>
<span class="token comment">#集群名称，节点之间要保持一致</span>
<span class="token key atrule">cluster.name</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>elasticsearch
<span class="token comment">#节点名称，集群内要唯一</span>
<span class="token key atrule">node.name</span><span class="token punctuation">:</span> node<span class="token punctuation">-</span><span class="token number">1002</span>
<span class="token key atrule">node.master</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token key atrule">node.data</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token comment">#ip地址</span>
<span class="token key atrule">network.host</span><span class="token punctuation">:</span> localhost
<span class="token comment">#http端口</span>
<span class="token key atrule">http.port</span><span class="token punctuation">:</span> <span class="token number">1002</span>
<span class="token comment">#tcp监听端口</span>
<span class="token key atrule">transport.tcp.port</span><span class="token punctuation">:</span> <span class="token number">9302</span>
<span class="token key atrule">discovery.seed_hosts</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"localhost:9301"</span><span class="token punctuation">]</span>
<span class="token key atrule">discovery.zen.fd.ping_timeout</span><span class="token punctuation">:</span> 1m
<span class="token key atrule">discovery.zen.fd.ping_retries</span><span class="token punctuation">:</span> <span class="token number">5</span>
<span class="token comment">#集群内的可以被选为主节点的节点列表</span>
<span class="token comment">#cluster.initial_master_nodes: ["node-1", "node-2","node-3"]</span>
<span class="token comment">#跨域配置</span>
<span class="token comment">#action.destructive_requires_name: true</span>
<span class="token key atrule">http.cors.enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token key atrule">http.cors.allow-origin</span><span class="token punctuation">:</span> <span class="token string">"*"</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><strong>node-1003节点</strong></p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token comment">#节点3的配置信息：</span>
<span class="token comment">#集群名称，节点之间要保持一致</span>
<span class="token key atrule">cluster.name</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>elasticsearch
<span class="token comment">#节点名称，集群内要唯一</span>
<span class="token key atrule">node.name</span><span class="token punctuation">:</span> node<span class="token punctuation">-</span><span class="token number">1003</span>
<span class="token key atrule">node.master</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token key atrule">node.data</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token comment">#ip地址</span>
<span class="token key atrule">network.host</span><span class="token punctuation">:</span> localhost
<span class="token comment">#http端口</span>
<span class="token key atrule">http.port</span><span class="token punctuation">:</span> <span class="token number">1003</span>
<span class="token comment">#tcp监听端口</span>
<span class="token key atrule">transport.tcp.port</span><span class="token punctuation">:</span> <span class="token number">9303</span>
<span class="token comment">#候选主节点的地址，在开启服务后可以被选为主节点</span>
<span class="token key atrule">discovery.seed_hosts</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"localhost:9301"</span><span class="token punctuation">,</span> <span class="token string">"localhost:9302"</span><span class="token punctuation">]</span>
<span class="token key atrule">discovery.zen.fd.ping_timeout</span><span class="token punctuation">:</span> 1m
<span class="token key atrule">discovery.zen.fd.ping_retries</span><span class="token punctuation">:</span> <span class="token number">5</span>
<span class="token comment">#集群内的可以被选为主节点的节点列表</span>
<span class="token comment">#cluster.initial_master_nodes: ["node-1", "node-2","node-3"]</span>
<span class="token comment">#跨域配置</span>
<span class="token comment">#action.destructive_requires_name: true</span>
<span class="token key atrule">http.cors.enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token key atrule">http.cors.allow-origin</span><span class="token punctuation">:</span> <span class="token string">"*"</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>三、如果有必要，删除每个节点中的data目录中所有内容。</p>
<h3 id="启动集群"><a href="#启动集群" class="headerlink" title="启动集群"></a>启动集群</h3><p>分别依次双击执行节点的bin&#x2F;elasticsearch.bat,启动节点服务器（可以编写一个脚本启动），启动后，会自动加入指定名称的集群。</p>
<h3 id="测试集群"><a href="#测试集群" class="headerlink" title="测试集群"></a>测试集群</h3><p>一、用Postman，查看集群状态</p>
<ol>
<li><code>GET http://127.0.0.1:1001/_cluster/health</code></li>
<li><code>GET http://127.0.0.1:1002/_cluster/health</code></li>
<li><code>GET http://127.0.0.1:1003/_cluster/health</code></li>
</ol>
<p>返回结果皆为如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"cluster_name"</span><span class="token operator">:</span> <span class="token string">"my-application"</span><span class="token punctuation">,</span>
    <span class="token property">"status"</span><span class="token operator">:</span> <span class="token string">"green"</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"number_of_nodes"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
    <span class="token property">"number_of_data_nodes"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
    <span class="token property">"active_primary_shards"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    <span class="token property">"active_shards"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    <span class="token property">"relocating_shards"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    <span class="token property">"initializing_shards"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    <span class="token property">"unassigned_shards"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    <span class="token property">"delayed_unassigned_shards"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    <span class="token property">"number_of_pending_tasks"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    <span class="token property">"number_of_in_flight_fetch"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    <span class="token property">"task_max_waiting_in_queue_millis"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    <span class="token property">"active_shards_percent_as_number"</span><span class="token operator">:</span> <span class="token number">100.0</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><strong>status字段</strong>指示着当前集群在总体上是否工作正常。它的三种颜色含义如下：</p>
<ol>
<li>green：所有的主分片和副本分片都正常运行。</li>
<li>yellow：所有的主分片都正常运行，但不是所有的副本分片都正常运行。</li>
<li>red：有主分片没能正常运行。</li>
</ol>
<p>二、用Postman，在一节点增加索引，另一节点获取索引</p>
<p>向集群中的node-1001节点增加索引：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#PUT http<span class="token operator">:</span><span class="token comment">//127.0.0.1:1001/user</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>返回结果：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"acknowledged"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    <span class="token property">"shards_acknowledged"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    <span class="token property">"index"</span><span class="token operator">:</span> <span class="token string">"user"</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>向集群中的node-1003节点获取索引：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#GET http<span class="token operator">:</span><span class="token comment">//127.0.0.1:1003/user</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>返回结果：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"user"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"aliases"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"mappings"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token property">"settings"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"index"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                <span class="token property">"creation_date"</span><span class="token operator">:</span> <span class="token string">"1617993035885"</span><span class="token punctuation">,</span>
                <span class="token property">"number_of_shards"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span>
                <span class="token property">"number_of_replicas"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span>
                <span class="token property">"uuid"</span><span class="token operator">:</span> <span class="token string">"XJKERwQlSJ6aUxZEN2EV0w"</span><span class="token punctuation">,</span>
                <span class="token property">"version"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"created"</span><span class="token operator">:</span> <span class="token string">"7080099"</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                <span class="token property">"provided_name"</span><span class="token operator">:</span> <span class="token string">"user"</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>如果在1003创建索引，同样在1001也能获取索引信息，这就是集群能力。</p>
<h2 id="31-环境-Linux单节点部署"><a href="#31-环境-Linux单节点部署" class="headerlink" title="31-环境-Linux单节点部署"></a>31-环境-Linux单节点部署</h2><h3 id="软件安装"><a href="#软件安装" class="headerlink" title="软件安装"></a>软件安装</h3><p>一、下载软件</p>
<p><a target="_blank" rel="noopener" href="https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-8-0">下载Linux版的Elasticsearch</a></p>
<p>二、解压软件</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 解压缩</span>
<span class="token function">tar</span> <span class="token parameter variable">-zxvf</span> elasticsearch-7.8.0-linux-x86_64.tar.gz <span class="token parameter variable">-C</span> /opt/module
<span class="token comment"># 改名</span>
<span class="token function">mv</span> elasticsearch-7.8.0 es
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>三、创建用户</p>
<p>因为安全问题，Elasticsearch不允许root用户直接运行，所以要创建新用户，在root用户中创建新用户。</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">useradd</span> es <span class="token comment">#新增es用户</span>
<span class="token function">passwd</span> es <span class="token comment">#为es用户设置密码</span>
<span class="token function">userdel</span> <span class="token parameter variable">-r</span> es <span class="token comment">#如果错了，可以删除再加</span>
<span class="token function">chown</span> <span class="token parameter variable">-R</span> es:es /opt/module/es <span class="token comment">#文件夹所有者</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>四、修改配置文件</p>
<p>修改&#x2F;opt&#x2F;module&#x2F;es&#x2F;config&#x2F;elasticsearch.yml文件。</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token comment"># 加入如下配置</span>
<span class="token key atrule">cluster.name</span><span class="token punctuation">:</span> elasticsearch
<span class="token key atrule">node.name</span><span class="token punctuation">:</span> node<span class="token punctuation">-</span><span class="token number">1</span>
<span class="token key atrule">network.host</span><span class="token punctuation">:</span> 0.0.0.0
<span class="token key atrule">http.port</span><span class="token punctuation">:</span> <span class="token number">9200</span>
<span class="token key atrule">cluster.initial_master_nodes</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"node-1"</span><span class="token punctuation">]</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>修改&#x2F;etc&#x2F;security&#x2F;limits.conf</p>
<pre class="line-numbers language-none"><code class="language-none"># 在文件末尾中增加下面内容
# 每个进程可以打开的文件数的限制
es soft nofile 65536
es hard nofile 65536
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>修改&#x2F;etc&#x2F;security&#x2F;limits.d&#x2F;20-nproc.conf</p>
<pre class="line-numbers language-none"><code class="language-none"># 在文件末尾中增加下面内容
# 每个进程可以打开的文件数的限制
es soft nofile 65536
es hard nofile 65536
# 操作系统级别对每个用户创建的进程数的限制
* hard nproc 4096
# 注： * 带表Linux所有用户名称
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>修改&#x2F;etc&#x2F;sysctl.conf</p>
<pre class="line-numbers language-none"><code class="language-none"># 在文件中增加下面内容
# 一个进程可以拥有的VMA(虚拟内存区域)的数量,默认值为65536
vm.max_map_count&#x3D;655360
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>重新加载</p>
<pre class="line-numbers language-none"><code class="language-none">sysctl -p
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h3 id="启动软件"><a href="#启动软件" class="headerlink" title="启动软件"></a>启动软件</h3><p>使用ES用户启动</p>
<pre class="line-numbers language-none"><code class="language-none">cd &#x2F;opt&#x2F;module&#x2F;es&#x2F;
#启动
bin&#x2F;elasticsearch
#后台启动
bin&#x2F;elasticsearch -d
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>启动时，会动态生成文件，如果文件所属用户不匹配，会发生错误，需要重新进行修改用户和用户组</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/75e24cd1fbc430577681bb4f8a0d3e2b.png" alt="img"></p>
<p>关闭防火墙</p>
<pre class="line-numbers language-none"><code class="language-none">#暂时关闭防火墙
systemctl stop firewalld
#永久关闭防火墙
systemctl enable firewalld.service #打开防火墙永久性生效，重启后不会复原
systemctl disable firewalld.service #关闭防火墙，永久性生效，重启后不会复原
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="测试软件"><a href="#测试软件" class="headerlink" title="测试软件"></a>测试软件</h3><p>浏览器中输入地址： <a target="_blank" rel="noopener" href="http://linux1:9200/">http://linux1:9200/</a></p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/477065a2cb499a32871549c21e2fc487.png" alt="img"></p>
<h2 id="32-环境-Linux集群部署"><a href="#32-环境-Linux集群部署" class="headerlink" title="32-环境-Linux集群部署"></a>32-环境-Linux集群部署</h2><h3 id="软件安装-1"><a href="#软件安装-1" class="headerlink" title="软件安装"></a>软件安装</h3><p>一、下载软件</p>
<p><a target="_blank" rel="noopener" href="https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-8-0">下载Linux版的Elasticsearch</a></p>
<p>二、解压软件</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 解压缩</span>
<span class="token function">tar</span> <span class="token parameter variable">-zxvf</span> elasticsearch-7.8.0-linux-x86_64.tar.gz <span class="token parameter variable">-C</span> /opt/module
<span class="token comment"># 改名</span>
<span class="token function">mv</span> elasticsearch-7.8.0 es-cluster
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>将软件分发到其他节点：linux2,linux3</p>
<p>三、创建用户</p>
<p>因为安全问题，Elasticsearch不允许root用户直接运行，所以要创建新用户，在root用户中创建新用户。</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">useradd</span> es <span class="token comment">#新增es用户</span>
<span class="token function">passwd</span> es <span class="token comment">#为es用户设置密码</span>
<span class="token function">userdel</span> <span class="token parameter variable">-r</span> es <span class="token comment">#如果错了，可以删除再加</span>
<span class="token function">chown</span> <span class="token parameter variable">-R</span> es:es /opt/module/es <span class="token comment">#文件夹所有者</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>四、修改配置文件</p>
<p>修改&#x2F;opt&#x2F;module&#x2F;es&#x2F;config&#x2F;elasticsearch.yml文件，分发文件。</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token comment"># 加入如下配置</span>
<span class="token comment">#集群名称</span>
<span class="token key atrule">cluster.name</span><span class="token punctuation">:</span> cluster<span class="token punctuation">-</span>es
<span class="token comment">#节点名称，每个节点的名称不能重复</span>
<span class="token key atrule">node.name</span><span class="token punctuation">:</span> node<span class="token punctuation">-</span><span class="token number">1</span>
<span class="token comment">#ip地址，每个节点的地址不能重复</span>
<span class="token key atrule">network.host</span><span class="token punctuation">:</span> linux1
<span class="token comment">#是不是有资格主节点</span>
<span class="token key atrule">node.master</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token key atrule">node.data</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token key atrule">http.port</span><span class="token punctuation">:</span> <span class="token number">9200</span>
<span class="token comment"># head插件需要这打开这两个配置</span>
<span class="token key atrule">http.cors.allow-origin</span><span class="token punctuation">:</span> <span class="token string">"*"</span>
<span class="token key atrule">http.cors.enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token key atrule">http.max_content_length</span><span class="token punctuation">:</span> 200mb
<span class="token comment">#es7.x之后新增的配置，初始化一个新的集群时需要此配置来选举master</span>
<span class="token key atrule">cluster.initial_master_nodes</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"node-1"</span><span class="token punctuation">]</span>
<span class="token comment">#es7.x之后新增的配置，节点发现</span>
<span class="token key atrule">discovery.seed_hosts</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"linux1:9300"</span><span class="token punctuation">,</span><span class="token string">"linux2:9300"</span><span class="token punctuation">,</span><span class="token string">"linux3:9300"</span><span class="token punctuation">]</span>
<span class="token key atrule">gateway.recover_after_nodes</span><span class="token punctuation">:</span> <span class="token number">2</span>
<span class="token key atrule">network.tcp.keep_alive</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token key atrule">network.tcp.no_delay</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token key atrule">transport.tcp.compress</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token comment">#集群内同时启动的数据任务个数，默认是2个</span>
<span class="token key atrule">cluster.routing.allocation.cluster_concurrent_rebalance</span><span class="token punctuation">:</span> <span class="token number">16</span>
<span class="token comment">#添加或删除节点及负载均衡时并发恢复的线程个数，默认4个</span>
<span class="token key atrule">cluster.routing.allocation.node_concurrent_recoveries</span><span class="token punctuation">:</span> <span class="token number">16</span>
<span class="token comment">#初始化数据恢复时，并发恢复线程的个数，默认4个</span>
<span class="token key atrule">cluster.routing.allocation.node_initial_primaries_recoveries</span><span class="token punctuation">:</span> <span class="token number">16</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>修改&#x2F;etc&#x2F;security&#x2F;limits.conf，分发文件</p>
<pre class="line-numbers language-none"><code class="language-none"># 在文件末尾中增加下面内容
es soft nofile 65536
es hard nofile 65536
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>修改&#x2F;etc&#x2F;security&#x2F;limits.d&#x2F;20-nproc.conf，分发文件</p>
<pre class="line-numbers language-none"><code class="language-none"># 在文件末尾中增加下面内容
es soft nofile 65536
es hard nofile 65536
\* hard nproc 4096
\# 注：*代表Linux所有用户名称
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>修改&#x2F;etc&#x2F;sysctl.conf</p>
<pre class="line-numbers language-none"><code class="language-none"># 在文件中增加下面内容
vm.max_map_count&#x3D;655360
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>重新加载</p>
<pre class="line-numbers language-none"><code class="language-none">sysctl -p
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h3 id="启动软件-1"><a href="#启动软件-1" class="headerlink" title="启动软件"></a>启动软件</h3><p>分别在不同节点上启动ES软件</p>
<pre class="line-numbers language-none"><code class="language-none">cd &#x2F;opt&#x2F;module&#x2F;es-cluster
#启动
bin&#x2F;elasticsearch
#后台启动
bin&#x2F;elasticsearch -d
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="测试集群-1"><a href="#测试集群-1" class="headerlink" title="测试集群"></a>测试集群</h3><p><img src="https://img-blog.csdnimg.cn/img_convert/0412e37cb5249d1ff0e813ee87f49a50.png" alt="img"></p>
<h1 id="第4章-Elasticsearch进阶"><a href="#第4章-Elasticsearch进阶" class="headerlink" title="第4章 Elasticsearch进阶"></a>第4章 Elasticsearch进阶</h1><h2 id="33-进阶-核心概念"><a href="#33-进阶-核心概念" class="headerlink" title="33-进阶-核心概念"></a>33-进阶-核心概念</h2><p><img src="https://img-blog.csdnimg.cn/img_convert/146a779da01f53e7f7a8d53132d3c7cf.png" alt="img"></p>
<h3 id="索引Index"><a href="#索引Index" class="headerlink" title="索引Index"></a>索引Index</h3><p>一个索引就是一个拥有几分相似特征的文档的集合。比如说，你可以有一个客户数据的索引，另一个产品目录的索引，还有一个订单数据的索引。一个索引由一个名字来标识（必须全部是小写字母），并且当我们要对这个索引中的文档进行索引、搜索、更新和删除（CRUD）的时候，都要使用到这个名字。在一个集群中，可以定义任意多的索引。</p>
<p>能搜索的数据必须索引，这样的好处是可以提高查询速度，比如：新华字典前面的目录就是索引的意思，目录可以提高查询速度。</p>
<p><strong>Elasticsearch索引的精髓：一切设计都是为了提高搜索的性能。</strong></p>
<h3 id="类型Type"><a href="#类型Type" class="headerlink" title="类型Type"></a>类型Type</h3><p>在一个索引中，你可以定义一种或多种类型。</p>
<p>一个类型是你的索引的一个逻辑上的分类&#x2F;分区，其语义完全由你来定。通常，会为具<br>有一组共同字段的文档定义一个类型。不同的版本，类型发生了不同的变化。</p>
<table>
<thead>
<tr>
<th>版本</th>
<th>Type</th>
</tr>
</thead>
<tbody><tr>
<td>5.x</td>
<td>支持多种 type</td>
</tr>
<tr>
<td>6.x</td>
<td>只能有一种 type</td>
</tr>
<tr>
<td>7.x</td>
<td>默认不再支持自定义索引类型（默认类型为： _doc）</td>
</tr>
</tbody></table>
<h3 id="文档Document"><a href="#文档Document" class="headerlink" title="文档Document"></a>文档Document</h3><p>一个文档是一个可被索引的基础信息单元，也就是一条数据。</p>
<p>比如：你可以拥有某一个客户的文档，某一个产品的一个文档，当然，也可以拥有某个订单的一个文档。文档以JSON（Javascript Object Notation）格式来表示，而JSON是一个到处存在的互联网数据交互格式。</p>
<p>在一个index&#x2F;type里面，你可以存储任意多的文档。</p>
<h3 id="字段Field"><a href="#字段Field" class="headerlink" title="字段Field"></a>字段Field</h3><p>相当于是数据表的字段，对文档数据根据不同属性进行的分类标识。</p>
<h3 id="映射Mapping"><a href="#映射Mapping" class="headerlink" title="映射Mapping"></a>映射Mapping</h3><p>mapping是处理数据的方式和规则方面做一些限制，如：某个字段的数据类型、默认值、分析器、是否被索引等等。这些都是映射里面可以设置的，其它就是处理ES里面数据的一些使用规则设置也叫做映射，按着最优规则处理数据对性能提高很大，因此才需要建立映射，并且需要思考如何建立映射才能对性能更好。</p>
<h3 id="分片Shards"><a href="#分片Shards" class="headerlink" title="分片Shards"></a>分片Shards</h3><p>一个索引可以存储超出单个节点硬件限制的大量数据。比如，一个具有10亿文档数据<br>的索引占据1TB的磁盘空间，而任一节点都可能没有这样大的磁盘空间。或者单个节点处理搜索请求，响应太慢。为了解决这个问题，<strong>Elasticsearch提供了将索引划分成多份的能力，每一份就称之为分片</strong>。当你创建一个索引的时候，你可以指定你想要的分片的数量。<strong>每个分片本身也是一个功能完善并且独立的“索引”</strong>，这个“索引”可以被放置到集群中的任何节点上。</p>
<p>分片很重要，主要有两方面的原因：</p>
<ol>
<li>允许你水平分割&#x2F;扩展你的内容容量。</li>
<li>允许你在分片之上进行分布式的、并行的操作，进而提高性能&#x2F;吞吐量。</li>
</ol>
<p>至于一个分片怎样分布，它的文档怎样聚合和搜索请求，是完全由Elasticsearch管理的，对于作为用户的你来说，这些都是透明的，无需过分关心。</p>
<p>被混淆的概念是，一个Lucene索引我们在Elasticsearch称作分片。一个Elasticsearch索引是分片的集合。当Elasticsearch在索引中搜索的时候，他发送查询到每一个属于索引的分片（Lucene索引），然后合并每个分片的结果到一个全局的结果集。</p>
<p>Lucene是Apache软件基金会Jakarta项目组的一个子项目，提供了一个简单却强大的应用程式接口，能够做全文索引和搜寻。在Java开发环境里Lucene是一个成熟的免费开源工具。就其本身而言，Lucene是当前以及最近几年最受欢迎的免费Java信息检索程序库。但Lucene只是一个提供全文搜索功能类库的核心工具包，而真正使用它还需要一个完善的服务框架搭建起来进行应用。</p>
<p>目前市面上流行的搜索引擎软件，主流的就两款：Elasticsearch和Solr,这两款都是基于Lucene搭建的，可以独立部署启动的搜索引擎服务软件。由于内核相同，所以两者除了服务器安装、部署、管理、集群以外，对于数据的操作修改、添加、保存、查询等等都十分类似。</p>
<h3 id="副本Replicas"><a href="#副本Replicas" class="headerlink" title="副本Replicas"></a>副本Replicas</h3><p>在一个网络&#x2F;云的环境里，失败随时都可能发生，在某个分片&#x2F;节点不知怎么的就处于<br>离线状态，或者由于任何原因消失了，这种情况下，有一个故障转移机制是非常有用并且是强烈推荐的。为此目的，Elasticsearch允许你创建分片的一份或多份拷贝，这些拷贝叫做复制分片(副本)。</p>
<p>复制分片之所以重要，有两个主要原因：</p>
<ul>
<li>在分片&#x2F;节点失败的情况下，<strong>提供了高可用性</strong>。因为这个原因，注意到复制分片从不与原&#x2F;主要（original&#x2F;primary）分片置于同一节点上是非常重要的。</li>
<li>扩展你的搜索量&#x2F;吞吐量，因为搜索可以在所有的副本上并行运行。</li>
</ul>
<p>总之，每个索引可以被分成多个分片。一个索引也可以被复制0次（意思是没有复制）或多次。一旦复制了，每个索引就有了主分片（作为复制源的原来的分片）和复制分片（主分片的拷贝）之别。</p>
<p>分片和复制的数量可以在索引创建的时候指定。在索引创建之后，你可以在任何时候动态地改变复制的数量，但是你事后不能改变分片的数量。</p>
<p>默认情况下，Elasticsearch中的每个索引被分片1个主分片和1个复制，这意味着，如果你的集群中至少有两个节点，你的索引将会有1个主分片和另外1个复制分片（1个完全拷贝），这样的话每个索引总共就有2个分片，我们需要根据索引需要确定分片个数。</p>
<h3 id="分配Allocation"><a href="#分配Allocation" class="headerlink" title="分配Allocation"></a>分配Allocation</h3><p>将分片分配给某个节点的过程，包括分配主分片或者副本。如果是副本，还包含从主分片复制数据的过程。这个过程是由master节点完成的。</p>
<h2 id="34-进阶-系统架构-简介"><a href="#34-进阶-系统架构-简介" class="headerlink" title="34-进阶-系统架构-简介"></a>34-进阶-系统架构-简介</h2><p><img src="https://img-blog.csdnimg.cn/img_convert/e4d13427545dc174eb9ccface85c1f0c.png" alt="img"></p>
<p>一个运行中的Elasticsearch实例称为一个节点，而集群是由一个或者多个拥有相同<br>cluster.name配置的节点组成，它们共同承担数据和负载的压力。当有节点加入集群中或者从集群中移除节点时，集群将会重新平均分布所有的数据。</p>
<p>当一个节点被选举成为主节点时，它将负责管理集群范围内的所有变更，例如增加、<br>删除索引，或者增加、删除节点等。而主节点并不需要涉及到文档级别的变更和搜索等操作，所以当集群只拥有一个主节点的情况下，即使流量的增加它也不会成为瓶颈。任何节点都可以成为主节点。我们的示例集群就只有一个节点，所以它同时也成为了主节点。</p>
<p>作为用户，我们可以将请求发送到集群中的任何节点，包括主节点。每个节点都知道<br>任意文档所处的位置，并且能够将我们的请求直接转发到存储我们所需文档的节点。无论我们将请求发送到哪个节点，它都能负责从各个包含我们所需文档的节点收集回数据，并将最终结果返回給客户端。Elasticsearch对这一切的管理都是透明的。</p>
<h2 id="35-进阶-单节点集群"><a href="#35-进阶-单节点集群" class="headerlink" title="35-进阶-单节点集群"></a>35-进阶-单节点集群</h2><p>我们在包含一个空节点的集群内创建名为users的索引，为了演示目的，我们将分配3个主分片和一份副本（每个主分片拥有一个副本分片）。</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#PUT http<span class="token operator">:</span><span class="token comment">//127.0.0.1:1001/users</span>
<span class="token punctuation">&#123;</span>
    <span class="token property">"settings"</span> <span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"number_of_shards"</span> <span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
        <span class="token property">"number_of_replicas"</span> <span class="token operator">:</span> <span class="token number">1</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>集群现在是拥有一个索引的单节点集群。所有3个主分片都被分配在node-1。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/54ee6753be248cc7d345b38a0eae7d96.png" alt="img"></p>
<p>通过elasticsearch-head插件（一个Chrome插件）查看集群情况。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/e8b15d0b243d486e91f478a220da63bf.png" alt="img"></p>
<ul>
<li>集群健康值:yellow(3of6)：表示当前集群的全部主分片都正常运行，但是副本分片没有全部处在正常状态。</li>
<li><img src="https://img-blog.csdnimg.cn/img_convert/489b6de480112879a00067b793bde685.png" alt="img">：3个主分片正常。</li>
<li><img src="https://img-blog.csdnimg.cn/img_convert/3ce9a78d26ee762f0a7a8abf7817a58e.png" alt="img">：3个副本分片都是Unassigned，它们都没有被分配到任何节点。在同一个节点上既保存原始数据又保存副本是没有意义的，因为一旦失去了那个节点，我们也将丢失该节点上的所有副本数据。</li>
</ul>
<p>当前集群是正常运行的，但存在丢失数据的风险。</p>
<hr>
<p><strong>elasticsearch-head chrome插件安装</strong></p>
<p><a target="_blank" rel="noopener" href="https://github.com/mobz/elasticsearch-head">插件获取网址</a>，下载压缩包，解压后将内容放入自定义命名为elasticsearch-head文件夹。</p>
<p>接着点击Chrome右上角选项-&gt;工具-&gt;管理扩展（或则地址栏输入chrome:&#x2F;&#x2F;extensions&#x2F;），选择打开“开发者模式”，让后点击“加载已解压得扩展程序”，选择elasticsearch-head&#x2F;_site，即可完成chrome插件安装。</p>
<h2 id="36-进阶-故障转移"><a href="#36-进阶-故障转移" class="headerlink" title="36-进阶-故障转移"></a>36-进阶-故障转移</h2><p>当集群中只有一个节点在运行时，意味着会有一个单点故障问题——没有冗余。幸运的是，我们只需再启动一个节点即可防止数据丢失。当你在同一台机器上启动了第二个节点时，只要它和第一个节点有同样的cluster.name配置，它就会自动发现集群并加入到其中。但是在不同机器上启动节点的时候，为了加入到同一集群，你需要配置一个可连接到的单播主机列表。之所以配置为使用单播发现，以防止节点无意中加入集群。只有在同一台机器上<br>运行的节点才会自动组成集群。</p>
<p>如果启动了第二个节点，集群将会拥有两个节点:所有主分片和副本分片都已被分配。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/bf76cb1bfbdf07555918d9055817ab44.png" alt="img"></p>
<p>通过elasticsearch-head插件查看集群情况</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/18db400822b83e727d6206f486b7b2ea.png" alt="img"></p>
<ul>
<li>集群健康值:green(3of6)：表示所有6个分片（包括3个主分片和3个副本分片）都在正常运行。</li>
<li><img src="https://img-blog.csdnimg.cn/img_convert/e485d8263a4aa3a94af0be951bd5a241.png" alt="img">：3个主分片正常。</li>
<li><img src="https://img-blog.csdnimg.cn/img_convert/e485d8263a4aa3a94af0be951bd5a241.png" alt="img">：第二个节点加入到集群后，3个副本分片将会分配到这个节点上——每个主分片对应一个副本分片。这意味着当集群内任何一个节点出现问题时，我们的数据都完好无损。所有新近被索引的文档都将会保存在主分片上，然后被并行的复制到对应的副本分片上。这就保证了我们既可以从主分片又可以从副本分片上获得文档。</li>
</ul>
<h2 id="37-进阶-水平扩容"><a href="#37-进阶-水平扩容" class="headerlink" title="37-进阶-水平扩容"></a>37-进阶-水平扩容</h2><p>怎样为我们的正在增长中的应用程序按需扩容呢？当启动了第三个节点，我们的集群将会拥有三个节点的集群:为了分散负载而对分片进行重新分配。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/d527e26aa2bccdf54b11410024eadc92.png" alt="img"></p>
<p>通过elasticsearch-head插件查看集群情况。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/6985fe14454c1269204478320d089bd7.png" alt="img"></p>
<ul>
<li>集群健康值:green(3of6)：表示所有6个分片（包括3个主分片和3个副本分片）都在正常运行。</li>
<li><img src="https://img-blog.csdnimg.cn/img_convert/9494419153adb44bedb395ac5d7bc488.png" alt="img">Node1和Node2上各有一个分片被迁移到了新的Node3节点，现在每个节点上都拥有2个分片，而不是之前的3个。这表示每个节点的硬件资源（CPU,RAM,I&#x2F;O）将被更少的分片所共享，每个分片的性能将会得到提升。</li>
</ul>
<p>分片是一个功能完整的搜索引擎，它拥有使用一个节点上的所有资源的能力。我们这个拥有6个分片（3个主分片和3个副本分片）的索引可以最大扩容到6个节点，每个节点上存在一个分片，并且每个分片拥有所在节点的全部资源。</p>
<p><strong>但是如果我们想要扩容超过6个节点怎么办呢？</strong></p>
<p>主分片的数目在索引创建时就已经确定了下来。实际上，这个数目定义了这个索引能够<br>存储 的最大数据量。（实际大小取决于你的数据、硬件和使用场景。）但是，读操作——<br>搜索和返回数据——可以同时被主分片或副本分片所处理，所以当你拥有越多的副本分片<br>时，也将拥有越高的吞吐量。</p>
<p>在运行中的集群上是可以动态调整副本分片数目的，我们可以按需伸缩集群。让我们把<br>副本数从默认的1增加到2。</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#PUT http<span class="token operator">:</span><span class="token comment">//127.0.0.1:1001/users/_settings</span>

<span class="token punctuation">&#123;</span>
    <span class="token property">"number_of_replicas"</span> <span class="token operator">:</span> <span class="token number">2</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>users索引现在拥有9个分片：3个主分片和6个副本分片。这意味着我们可以将集群<br>扩容到9个节点，每个节点上一个分片。相比原来3个节点时，集群搜索性能可以提升3倍。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/97fd01e34e5d8df23d226c4fef157801.png" alt="img"></p>
<p>通过elasticsearch-head插件查看集群情况：</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/8bf9dbf0cec5b7875bf8aa9d17a9a67c.png" alt="img"></p>
<p>当然，如果只是在相同节点数目的集群上增加更多的副本分片并不能提高性能，因为每<br>个分片从节点上获得的资源会变少。你需要增加更多的硬件资源来提升吞吐量。</p>
<p>但是更多的副本分片数提高了数据冗余量：按照上面的节点配置，我们可以在失去 2 个节点<br>的情况下不丢失任何数据。</p>
<h2 id="38-进阶-应对故障"><a href="#38-进阶-应对故障" class="headerlink" title="38-进阶-应对故障"></a>38-进阶-应对故障</h2><p>我们关闭第一个节点，这时集群的状态为:关闭了一个节点后的集群。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/44e841004be934e6bce08187ca3852bb.png" alt="img"></p>
<p>我们关闭的节点是一个主节点。而集群必须拥有一个主节点来保证正常工作，所以发生<br>的第一件事情就是选举一个新的主节点：Node2。在我们关闭Node1的同时也失去了主<br>分片1和2，并且在缺失主分片的时候索引也不能正常工作。如果此时来检查集群的状况，我们看到的状态将会为red：不是所有主分片都在正常工作。</p>
<p>幸运的是，在其它节点上存在着这两个主分片的完整副本，所以新的主节点立即将这些分片在Node2和Node3上对应的副本分片提升为主分片，此时集群的状态将会为yellow。这个提升主分片的过程是瞬间发生的，如同按下一个开关一般。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/e956bda7e0d005699e27760d4193d101.png" alt="img"></p>
<p><strong>为什么我们集群状态是yellow而不是green呢？</strong></p>
<p>虽然我们拥有所有的三个主分片，但是同时设置了每个主分片需要对应2份副本分片，而此<br>时只存在一份副本分片。所以集群不能为green的状态，不过我们不必过于担心：如果我<br>们同样关闭了Node2，我们的程序依然可以保持在不丢任何数据的情况下运行，因为<br>Node3为每一个分片都保留着一份副本。</p>
<p>如果想回复原来的样子，要确保Node-1的配置文件有如下配置：</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">discovery.seed_hosts</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"localhost:9302"</span><span class="token punctuation">,</span> <span class="token string">"localhost:9303"</span><span class="token punctuation">]</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>集群可以将缺失的副本分片再次进行分配，那么集群的状态也将恢复成之前的状态。如果Node1依然拥有着之前的分片，它将尝试去重用它们，同时仅从主分片复制发生了修改的数据文件。和之前的集群相比，只是Master节点切换了。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/37eea6a8dae7ba908312f2ebf0eced11.png" alt="img"></p>
<h2 id="39-进阶-路由计算-amp-分片控制"><a href="#39-进阶-路由计算-amp-分片控制" class="headerlink" title="39-进阶-路由计算 &amp; 分片控制"></a>39-进阶-路由计算 &amp; 分片控制</h2><h3 id="路由计算"><a href="#路由计算" class="headerlink" title="路由计算"></a>路由计算</h3><p>当索引一个文档的时候，文档会被存储到一个主分片中。Elasticsearch如何知道一个<br>文档应该存放到哪个分片中呢？当我们创建文档时，它如何决定这个文档应当被存储在分片1还是分片2中呢？首先这肯定不会是随机的，否则将来要获取文档的时候我们就不知道从何处寻找了。实际上，这个过程是根据下面这个公式决定的：</p>
<pre class="line-numbers language-none"><code class="language-none">shard &#x3D; hash(routing) % number_of_primary_shards
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>routing是一个可变值，默认是文档的_id，也可以设置成一个自定义的值。routing通过hash函数生成一个数字，然后这个数字再除以number_of_primary_shards（主分片的数量）后得到余数。这个分布在0到number_of_primary_shards-1之间的余数，就是我们所寻求的文档所在分片的位置。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/9c34e8603887c2bed475416e3b67cd9a.png" alt="img"></p>
<p>这就解释了为什么我们要在创建索引的时候就确定好主分片的数量并且永远不会改变这个数量:因为如果数量变化了，那么所有之前路由的值都会无效，文档也再也找不到了。</p>
<p>所有的文档API(get.index.delete、bulk,update以及mget）都接受一个叫做routing的路由参数，通过这个参数我们可以自定义文档到分片的映射。一个自定义的路由参数可以用来确保所有相关的文档—一例如所有属于同一个用户的文档——都被存储到同一个分片中。</p>
<h3 id="分片控制"><a href="#分片控制" class="headerlink" title="分片控制"></a>分片控制</h3><p>我们可以发送请求到集群中的任一节点。每个节点都有能力处理任意请求。每个节点都知道集群中任一文档位置，所以可以直接将请求转发到需要的节点上。在下面的例子中，如果将所有的请求发送到Node 1001，我们将其称为协调节点<strong>coordinating node</strong>。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/3940d6cdb197259368542b86384911a4.png" alt="img"></p>
<p>当发送请求的时候，为了扩展负载，更好的做法是轮询集群中所有的节点。</p>
<h2 id="40-进阶-数据写流程"><a href="#40-进阶-数据写流程" class="headerlink" title="40-进阶-数据写流程"></a>40-进阶-数据写流程</h2><p>新建、索引和删除请求都是写操作，必须在主分片上面完成之后才能被复制到相关的副本分片。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/418356a32516c222a8d366df021276c2.png" alt="img"></p>
<p>在客户端收到成功响应时，文档变更已经在主分片和所有副本分片执行完成，变更是安全的。有一些可选的<strong>请求参数</strong>允许您影响这个过程，可能以数据安全为代价提升性能。这些选项很少使用，因为Elasticsearch已经很快，但是为了完整起见，请参考下文：</p>
<ol>
<li>consistency</li>
</ol>
<ul>
<li>即一致性。在默认设置下，即使仅仅是在试图执行一个写操作之前，主分片都会要求必须要有规定数量quorum（或者换种说法，也即必须要有大多数）的分片副本处于活跃可用状态，才会去执行写操作（其中分片副本可以是主分片或者副本分片）。这是为了避免在发生网络分区故障（network partition）的时候进行写操作，进而导致数据不一致。规定数量即：<strong>int((primary+number_of_replicas)&#x2F;2)+1</strong></li>
<li>consistency参数的值可以设为：<ul>
<li>one：只要主分片状态ok就允许执行写操作。</li>
<li>all：必须要主分片和所有副本分片的状态没问题才允许执行写操作。</li>
<li>quorum：默认值为quorum,即大多数的分片副本状态没问题就允许执行写操作。</li>
</ul>
</li>
<li>注意，规定数量的计算公式中number_of_replicas指的是在索引设置中的设定副本分片数，而不是指当前处理活动状态的副本分片数。如果你的索引设置中指定了当前索引拥有3个副本分片，那规定数量的计算结果即：<strong>int((1 primary + 3 replicas) &#x2F; 2) + 1 &#x3D; 3</strong>，如果此时你只启动两个节点，那么处于活跃状态的分片副本数量就达不到规定数量，也因此您将无法索引和删除任何文档。</li>
</ul>
<ol>
<li>timeout<ul>
<li>如果没有足够的副本分片会发生什么？Elasticsearch会等待，希望更多的分片出现。默认情况下，它最多等待1分钟。如果你需要，你可以使用timeout参数使它更早终止：100是100毫秒，30s是30秒。</li>
</ul>
</li>
</ol>
<p>新索引默认有1个副本分片，这意味着为满足规定数量应该需要两个活动的分片副本。但是，这些默认的设置会阻止我们在单一节点上做任何事情。为了避免这个问题，要求只有当number_of_replicas大于1的时候，规定数量才会执行。</p>
<h2 id="41-进阶-数据读流程"><a href="#41-进阶-数据读流程" class="headerlink" title="41-进阶-数据读流程"></a>41-进阶-数据读流程</h2><p><img src="https://img-blog.csdnimg.cn/img_convert/7139df83ee6f7a59c5d3252d34cc8762.png" alt="img"></p>
<p>在处理读取请求时，协调结点在每次请求的时候都会通过轮询所有的副本分片来达到负载均衡。在文档被检索时，已经被索引的文档可能已经存在于主分片上但是还没有复制到副本分片。在这种情况下，副本分片可能会报告文档不存在，但是主分片可能成功返回文档。一旦索引请求成功返回给用户，文档在主分片和副本分片都是可用的。</p>
<h2 id="42-进阶-更新流程-amp-批量操作流程"><a href="#42-进阶-更新流程-amp-批量操作流程" class="headerlink" title="42-进阶-更新流程 &amp; 批量操作流程"></a>42-进阶-更新流程 &amp; 批量操作流程</h2><h3 id="更新流程"><a href="#更新流程" class="headerlink" title="更新流程"></a>更新流程</h3><p>部分更新一个文档结合了先前说明的读取和写入流程：</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/ca41993144aee98311671278725437cd.png" alt="img"></p>
<p>部分更新一个文档的步骤如下：</p>
<ol>
<li>客户端向Node 1发送更新请求。</li>
<li>它将请求转发到主分片所在的Node 3。</li>
<li>Node 3从主分片检索文档，修改_source字段中的JSON，并且尝试重新索引主分片的文档。如果文档已经被另一个进程修改,它会重试步骤3,超过retry_on_conflict次后放弃。</li>
<li>如果Node 3成功地更新文档，它将新版本的文档并行转发到Node1和Node2上的副本分片，重新建立索引。一旦所有副本分片都返回成功，Node3向协调节点也返回成功，协调节点向客户端返回成功。</li>
</ol>
<p>当主分片把更改转发到副本分片时，它不会转发更新请求。相反，它转发完整文档的新版本。请记住，这些更改将会异步转发到副本分片，并且不能保证它们以发送它们相同的顺序到达。如果Elasticsearch仅转发更改请求，则可能以错误的顺序应用更改，导致得到损坏的文档。</p>
<h3 id="批量操作流程"><a href="#批量操作流程" class="headerlink" title="批量操作流程"></a>批量操作流程</h3><p><strong>mget和bulkAPI的模式类似于单文档模式</strong>。区别在于协调节点知道每个文档存在于哪个分片中。它将整个多文档请求分解成每个分片的多文档请求，并且将这些请求并行转发到每个参与节点。</p>
<p>协调节点一旦收到来自每个节点的应答，就将每个节点的响应收集整理成单个响应，返回给客户端。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/b90ea9c79138d8361ca339cff205fdb0.png" alt="img"></p>
<p><strong>用单个 mget 请求取回多个文档所需的步骤顺序:</strong></p>
<ol>
<li>客户端向Node1发送mget请求。</li>
<li>Node1为每个分片构建多文档获取请求，然后并行转发这些请求到托管在每个所需的主分片或者副本分片的节点上。一旦收到所有答复，Node1构建响应并将其返回给客户端。</li>
</ol>
<p>可以对docs数组中每个文档设置routing参数。</p>
<p>bulk API，允许在单个批量请求中执行多个创建、索引、删除和更新请求。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/83499315a7b8ab81471a88f3e142f0a8.png" alt="img"></p>
<p><strong>bulk API按如下步骤顺序执行：</strong></p>
<ol>
<li>客户端向Node1发送bulk请求。</li>
<li>Node 1为每个节点创建一个批量请求，并将这些请求并行转发到每个包含主分片的节点主机。</li>
<li>主分片一个接一个按顺序执行每个操作。当每个操作成功时,主分片并行转发新文档（或删除）到副本分片，然后执行下一个操作。一旦所有的副本分片报告所有操作成功，该节点将向协调节点报告成功，协调节点将这些响应收集整理并返回给客户端。</li>
</ol>
<h2 id="43-进阶-倒排索引"><a href="#43-进阶-倒排索引" class="headerlink" title="43-进阶-倒排索引"></a>43-进阶-倒排索引</h2><p>分片是Elasticsearch最小的工作单元。但是究竟什么是一个分片，它是如何工作的？</p>
<p>传统的数据库每个字段存储单个值，但这对全文检索并不够。文本字段中的每个单词需要被搜索，对数据库意味着需要单个字段有索引多值的能力。最好的支持是一个字段多个值需求的数据结构是<strong>倒排索引</strong>。</p>
<h3 id="倒排索引原理"><a href="#倒排索引原理" class="headerlink" title="倒排索引原理"></a>倒排索引原理</h3><p>Elasticsearch使用一种称为倒排索引的结构，它适用于快速的全文搜索。</p>
<p>见其名，知其意，有倒排索引，肯定会对应有正向索引。正向索引（forward index），反向索引（inverted index）更熟悉的名字是<strong>倒排索引</strong>。</p>
<p>所谓的<strong>正向索引</strong>，就是搜索引擎会将待搜索的文件都对应一个文件ID，搜索时将这个ID和搜索关键字进行对应，形成K-V对，然后对关键字进行统计计数。（统计？？下文有解释）</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/cba02cc6d7c5f054dfe5d58fafac9a6a.png" alt="img"></p>
<p>但是互联网上收录在搜索引擎中的文档的数目是个天文数字，这样的索引结构根本无法满足实时返回排名结果的要求。所以，搜索引擎会将正向索引重新构建为倒排索引，即把文件ID对应到关键词的映射转换为关键词到文件ID的映射，每个关键词都对应着一系列的文件，这些文件中都出现这个关键词。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/a1f52e96e0ac218b5024d708202afba4.png" alt="img"></p>
<h3 id="倒排索引的例子"><a href="#倒排索引的例子" class="headerlink" title="倒排索引的例子"></a>倒排索引的例子</h3><p>一个倒排索引由文档中所有不重复词的列表构成，对于其中每个词，有一个包含它的文档列表。例如，假设我们有两个文档，每个文档的content域包含如下内容：</p>
<ul>
<li>The quick brown fox jumped over the lazy dog</li>
<li>Quick brown foxes leap over lazy dogs in summer</li>
</ul>
<p>为了创建倒排索引，我们首先将每个文档的content域拆分成单独的词（我们称它为词条或tokens)，创建一个包含所有不重复词条的排序列表，然后列出每个词条出现在哪个文档。结果如下所示：</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/3cc642e9bae776c3e617f9d117d41e21.png" alt="img"></p>
<p>现在，如果我们想搜索<code>quick</code> <code>brown</code>，我们只需要查找包含每个词条的文档：</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/f26aaa01e011edfa68736956b2f1ddea.png" alt="img"></p>
<p>两个文档都匹配，但是第一个文档比第二个匹配度更高。如果我们使用仅计算匹配词条数量的简单相似性算法，那么我们可以说，对于我们查询的相关性来讲，第一个文档比第二个文档更佳。</p>
<p>但是，我们目前的倒排索引有一些问题：</p>
<ul>
<li><code>Quick</code>和<code>quick</code>以独立的词条出现，然而用户可能认为它们是相同的词。</li>
<li><code>fox</code>和<code>foxes</code>非常相似，就像<code>dog</code>和<code>dogs</code>；他们有相同的词根。</li>
<li><code>jumped</code>和<code>leap</code>，尽管没有相同的词根，但他们的意思很相近。他们是同义词。</li>
</ul>
<p>使用前面的索引搜索<code>+Quick</code> <code>+fox</code>不会得到任何匹配文档。(记住，＋前缀表明这个词必须存在）。</p>
<p>只有同时出现<code>Quick</code>和<code>fox</code> 的文档才满足这个查询条件，但是第一个文档包含<code>quick</code> <code>fox</code> ，第二个文档包含<code>Quick</code> <code>foxes</code> 。</p>
<p>我们的用户可以合理的期望两个文档与查询匹配。我们可以做的更好。</p>
<p>如果我们将词条规范为标准模式，那么我们可以找到与用户搜索的词条不完全一致，但具有足够相关性的文档。例如：</p>
<ul>
<li><code>Quick</code>可以小写化为<code>quick</code>。</li>
<li><code>foxes</code>可以词干提取变为词根的格式为<code>fox</code>。类似的，<code>dogs</code>可以为提取为<code>dog</code>。</li>
<li><code>jumped</code>和<code>leap</code>是同义词，可以索引为相同的单词<code>jump</code> 。</li>
</ul>
<p>现在索引看上去像这样：</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/19813d1918c89461303377444cf85c8c.png" alt="img"></p>
<p>这还远远不够。我们搜索<code>+Quick</code> <code>+fox</code> 仍然会失败，因为在我们的索引中，已经没有<code>Quick</code>了。但是，如果我们对搜索的字符串使用与content域相同的标准化规则，会变成查询<code>+quick</code> <code>+fox</code>，这样两个文档都会匹配！分词和标准化的过程称为<strong>分析</strong>，这非常重要。你只能搜索在索引中出现的词条，所以索引文本和查询字符串必须标准化为相同的格式。</p>
<h2 id="44-进阶-文档搜索"><a href="#44-进阶-文档搜索" class="headerlink" title="44-进阶-文档搜索"></a>44-进阶-文档搜索</h2><h3 id="不可改变的倒排索引"><a href="#不可改变的倒排索引" class="headerlink" title="不可改变的倒排索引"></a>不可改变的倒排索引</h3><p>早期的全文检索会为整个文档集合建立一个很大的倒排索引并将其写入到磁盘。一旦新的索引就绪，旧的就会被其替换，这样最近的变化便可以被检索到。</p>
<p>倒排索引被写入磁盘后是不可改变的：它永远不会修改。</p>
<ul>
<li>不需要锁。如果你从来不更新索引，你就不需要担心多进程同时修改数据的问题。</li>
<li>一旦索引被读入内核的文件系统缓存，便会留在哪里，由于其不变性。只要文件系统缓存中还有足够的空间，那么大部分读请求会直接请求内存，而不会命中磁盘。这提供了很大的性能提升。</li>
<li>其它缓存(像filter缓存)，在索引的生命周期内始终有效。它们不需要在每次数据改变时被重建，因为数据不会变化。</li>
<li>写入单个大的倒排索引允许数据被压缩，减少磁盘IO和需要被缓存到内存的索引的使用量。</li>
</ul>
<p>当然，一个不变的索引也有不好的地方。主要事实是它是不可变的!你不能修改它。如果你需要让一个新的文档可被搜索，你需要重建整个索引。这要么对一个索引所能包含的数据量造成了很大的限制，要么对索引可被更新的频率造成了很大的限制。</p>
<h3 id="动态更新索引"><a href="#动态更新索引" class="headerlink" title="动态更新索引"></a>动态更新索引</h3><p>如何在保留不变性的前提下实现倒排索引的更新？</p>
<p>答案是：用更多的索引。通过增加新的补充索引来反映新近的修改，而不是直接重写整个倒排索引。每一个倒排索引都会被轮流查询到,从最早的开始查询完后再对结果进行合并。</p>
<p>Elasticsearch基于Lucene，这个java库引入了<strong>按段搜索</strong>的概念。每一段本身都是一个倒排索引，但索引在Lucene中除表示所有段的集合外，还增加了提交点的概念—一个列出了所有已知段的文件。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/9ee1adbb2d55e710257e01b812a6d8cf.png" alt="img"></p>
<p>按段搜索会以如下流程执行：</p>
<p>一、新文档被收集到内存索引缓存。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/9d499fde966ee9825fa5a424d8357489.png" alt="img"></p>
<p>二、不时地,缓存被提交。</p>
<ol>
<li>一个新的段，一个追加的倒排索引，被写入磁盘。</li>
<li>一个新的包含新段名字的提交点被写入磁盘。</li>
<li>磁盘进行同步，所有在文件系统缓存中等待的写入都刷新到磁盘，以确保它们被写入物理文件</li>
</ol>
<p>三、新的段被开启，让它包含的文档可见以被搜索。</p>
<p>四、内存缓存被清空，等待接收新的文档。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/f74828ff58cc4635a97e88706a221e50.png" alt="img"></p>
<p>当一个查询被触发，所有已知的段按顺序被查询。词项统计会对所有段的结果进行聚合，以保证每个词和每个文档的关联都被准确计算。这种方式可以用相对较低的成本将新文档添加到索引。</p>
<p>段是不可改变的，所以既不能从把文档从旧的段中移除，也不能修改旧的段来进行反映文档的更新。取而代之的是，每个提交点会包含一个.del文件，文件中会列出这些被删除文档的段信息。</p>
<p>当一个<strong>文档被“删除”</strong>时，它实际上只是在.del文件中被标记删除。一个被标记删除的文档仍然可以被查询匹配到，但它会在最终结果被返回前从结果集中移除。</p>
<p><strong>文档更新</strong>也是类似的操作方式:当一个文档被更新时，旧版本文档被标记删除，文档的新版本被索引到一个新的段中。可能两个版本的文档都会被一个查询匹配到，但被删除的那个旧版本文档在结果集返回前就已经被移除。</p>
<h2 id="45-进阶-文档刷新-amp-文档刷写-amp-文档合并"><a href="#45-进阶-文档刷新-amp-文档刷写-amp-文档合并" class="headerlink" title="45-进阶-文档刷新 &amp; 文档刷写 &amp; 文档合并"></a>45-进阶-文档刷新 &amp; 文档刷写 &amp; 文档合并</h2><p><img src="https://img-blog.csdnimg.cn/img_convert/b3b31c1e592d5aa794e7c9fcb259c924.png" alt="img"></p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/521c25f0f16247240234d1b8eb3c5f25.png" alt="img"></p>
<h3 id="近实时搜索"><a href="#近实时搜索" class="headerlink" title="近实时搜索"></a>近实时搜索</h3><p>随着按段（per-segment）搜索的发展，一个新的文档从索引到可被搜索的延迟显著降低了。新文档在几分钟之内即可被检索，但这样还是不够快。磁盘在这里成为了瓶颈。<strong>提交（Commiting）一个新的段到磁盘需要一个fsync来确保段被物理性地写入磁盘</strong>，这样在断电的时候就不会丢失数据。但是fsync操作代价很大；如果每次索引一个文档都去执行一次的话会造成很大的性能问题。</p>
<p>我们需要的是一个更轻量的方式来使一个文档可被搜索，这意味着fsync要从整个过程中被移除。在Elasticsearch和磁盘之间是<strong>文件系统缓存</strong>。像之前描述的一样，在内存索引缓冲区中的文档会被写入到一个新的段中。但是这里新段会被先写入到文件系统缓存—这一步代价会比较低，稍后再被刷新到磁盘—这一步代价比较高。不过只要文件已经在缓存中，就可以像其它文件一样被打开和读取了。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/a679d4f5f4bfa6913a53316251beef2a.png" alt="img"></p>
<p>Lucene允许新段被写入和打开，使其包含的文档在未进行一次完整提交时便对搜索可见。这种方式比进行一次提交代价要小得多，并且在不影响性能的前提下可以被频繁地执行。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/673d3a77e254fa3a5a6f5293ffb125ab.png" alt="img"></p>
<p>在Elasticsearch中，写入和打开一个新段的轻量的过程叫做refresh。默认情况下每个分片会每秒自动刷新一次。这就是为什么我们说Elasticsearch是近实时搜索：文档的变化并不是立即对搜索可见，但会在一秒之内变为可见。</p>
<p>这些行为可能会对新用户造成困惑：他们索引了一个文档然后尝试搜索它，但却没有搜到。这个问题的解决办法是用refresh API执行一次手动刷新：&#x2F;usersl_refresh</p>
<p>尽管刷新是比提交轻量很多的操作，它还是会有性能开销。当写测试的时候，手动刷新很有用，但是不要在生产环境下每次索引一个文档都去手动刷新。相反，你的应用需要意识到Elasticsearch 的近实时的性质，并接受它的不足。</p>
<p>并不是所有的情况都需要每秒刷新。可能你正在使用Elasticsearch索引大量的日志文件，你可能想优化索引速度而不是近实时搜索，可以通过设置refresh_interval，降低每个索引的刷新频率</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"settings"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
    	<span class="token property">"refresh_interval"</span><span class="token operator">:</span> <span class="token string">"30s"</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>refresh_interval可以在既存索引上进行动态更新。在生产环境中，当你正在建立一个大的新索引时，可以先关闭自动刷新，待开始使用该索引时，再把它们调回来。</p>
<pre class="line-numbers language-none"><code class="language-none"># 关闭自动刷新
PUT &#x2F;users&#x2F;_settings
&#123; &quot;refresh_interval&quot;: -1 &#125;

# 每一秒刷新
PUT &#x2F;users&#x2F;_settings
&#123; &quot;refresh_interval&quot;: &quot;1s&quot; &#125;
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="持久化变更"><a href="#持久化变更" class="headerlink" title="持久化变更"></a>持久化变更</h3><p>如果没有用fsync把数据从文件系统缓存刷（flush）到硬盘，我们不能保证数据在断电甚至是程序正常退出之后依然存在。为了保证Elasticsearch的可靠性，需要确保数据变化被持久化到磁盘。在动态更新索引，我们说一次完整的提交会将段刷到磁盘，并写入一个包含所有段列表的提交点。Elasticsearch在启动或重新打开一个索引的过程中使用这个提交点来判断哪些段隶属于当前分片。</p>
<p>即使通过每秒刷新(refresh）实现了近实时搜索，我们仍然需要经常进行完整提交来确保能从失败中恢复。但在两次提交之间发生变化的文档怎么办?我们也不希望丢失掉这些数据。Elasticsearch增加了一个translog，或者叫事务日志，在每一次对Elasticsearch进行操作时均进行了日志记录。</p>
<p>整个流程如下:</p>
<p>一、一个文档被索引之后，就会被添加到内存缓冲区，并且追加到了translog</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/baeab48c8d6b87660ac4fb954e9c9731.png" alt="img"></p>
<p>二、刷新（refresh）使分片每秒被刷新（refresh）一次：</p>
<ul>
<li>这些在内存缓冲区的文档被写入到一个新的段中，且没有进行fsync操作。</li>
<li>这个段被打开，使其可被搜索。</li>
<li>内存缓冲区被清空。</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/img_convert/17be4247e6b23f31b1e589c70d61e817.png" alt="img"></p>
<p>三、这个进程继续工作，更多的文档被添加到内存缓冲区和追加到事务日志。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/4b5c4a3a3ffb4c84625bb283f6a67018.png" alt="img"></p>
<p>四、每隔一段时间—例如translog变得越来越大，索引被刷新（flush）；一个新的translog被创建，并且一个全量提交被执行。</p>
<ul>
<li>所有在内存缓冲区的文档都被写入一个新的段。</li>
<li>缓冲区被清空。</li>
<li>一个提交点被写入硬盘。</li>
<li>文件系统缓存通过fsync被刷新（flush）。</li>
<li>老的translog被删除。</li>
</ul>
<p>translog提供所有还没有被刷到磁盘的操作的一个持久化纪录。当Elasticsearch启动的时候，它会从磁盘中使用最后一个提交点去恢复己知的段，并且会重放translog中所有在最后一次提交后发生的变更操作。</p>
<p>translog也被用来提供实时CRUD。当你试着通过ID查询、更新、删除一个文档，它会在尝试从相应的段中检索之前，首先检查translog任何最近的变更。这意味着它总是能够实时地获取到文档的最新版本。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/11c7d2cc05244e669eb8402dd8049de9.png" alt="img"></p>
<p>执行一个提交并且截断translog的行为在Elasticsearch被称作一次flush。分片每30分钟被自动刷新（flush)，或者在translog太大的时候也会刷新。</p>
<p>你很少需要自己手动执行flush操作，通常情况下，自动刷新就足够了。这就是说，在重启节点或关闭索引之前执行flush有益于你的索引。当Elasticsearch尝试恢复或重新打开一个索引，它需要重放translog中所有的操作，所以如果日志越短，恢复越快。</p>
<p>translog的目的是保证操作不会丢失，在文件被fsync到磁盘前，被写入的文件在重启之后就会丢失。默认translog是每5秒被fsync刷新到硬盘，或者在每次写请求完成之后执行（e.g.index,delete,update,bulk）。这个过程在主分片和复制分片都会发生。最终，基本上，这意味着在整个请求被fsync到主分片和复制分片的translog之前，你的客户端不会得到一个200 OK响应。</p>
<p>在每次请求后都执行一个fsync会带来一些性能损失，尽管实践表明这种损失相对较小（特别是bulk导入，它在一次请求中平摊了大量文档的开销）。</p>
<p>但是对于一些大容量的偶尔丢失几秒数据问题也并不严重的集群，使用异步的fsync还是比较有益的。比如，写入的数据被缓存到内存中，再每5秒执行一次fsync。如果你决定使用异步translog的话，你需要保证在发生crash时，丢失掉sync_interval时间段的数据也无所谓。请在决定前知晓这个特性。如果你不确定这个行为的后果，最好是使用默认的参数{“index.translog.durability”:“request”}来避免数据丢失。</p>
<h3 id="段合并"><a href="#段合并" class="headerlink" title="段合并"></a>段合并</h3><p>由于自动刷新流程每秒会创建一个新的段，这样会导致短时间内的段数量暴增。而段数目太多会带来较大的麻烦。每一个段都会消耗文件句柄、内存和cpu运行周期。更重要的是，每个搜索请求都必须轮流检查每个段；所以段越多，搜索也就越慢。</p>
<p>Elasticsearch通过在后台进行段合并来解决这个问题。小的段被合并到大的段，然后这些大的段再被合并到更大的段。</p>
<p>段合并的时候会将那些旧的已删除文档从文件系统中清除。被删除的文档（或被更新文档的旧版本）不会被拷贝到新的大段中。</p>
<p>启动段合并不需要你做任何事。进行索引和搜索时会自动进行。</p>
<p>一、当索引的时候，刷新（refresh）操作会创建新的段并将段打开以供搜索使用。</p>
<p>二、合并进程选择一小部分大小相似的段，并且在后台将它们合并到更大的段中。这并不会中断索引和搜索。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/c907ca35bd7c0393d46aec2c7038af19.png" alt="img"></p>
<p>三、一旦合并结束，老的段被删除</p>
<ul>
<li>新的段被刷新(flush)到了磁盘。</li>
<li>写入一个包含新段且排除旧的和较小的段的新提交点。</li>
<li>新的段被打开用来搜索。老的段被删除。</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/img_convert/a00cc1c19652c47fcfb663aaf337a41b.png" alt="img"></p>
<p>合并大的段需要消耗大量的I&#x2F;O和CPU资源，如果任其发展会影响搜索性能。Elasticsearch在默认情况下会对合并流程进行资源限制，所以搜索仍然有足够的资源很好地执行。</p>
<h2 id="46-进阶-文档分析"><a href="#46-进阶-文档分析" class="headerlink" title="46-进阶-文档分析"></a>46-进阶-文档分析</h2><p>分析包含下面的过程：</p>
<ul>
<li>将一块文本分成适合于倒排索引的独立的词条。</li>
<li>将这些词条统一化为标准格式以提高它们的“可搜索性”，或者recall。</li>
</ul>
<p>分析器执行上面的工作。分析器实际上是将三个功能封装到了一个包里：</p>
<ul>
<li>字符过滤器：首先，字符串按顺序通过每个字符过滤器。他们的任务是在分词前整理字符串。一个字符过滤器可以用来去掉HTML，或者将&amp;转化成and。</li>
<li>分词器：其次，字符串被分词器分为单个的词条。一个简单的分词器遇到空格和标点的时候，可能会将文本拆分成词条。</li>
<li>Token过滤器：最后，词条按顺序通过每个token过滤器。这个过程可能会改变词条（例如，小写化Quick），删除词条（例如，像a，and，the等无用词），或者增加词条（例如，像jump和leap这种同义词）</li>
</ul>
<h3 id="内置分析器"><a href="#内置分析器" class="headerlink" title="内置分析器"></a>内置分析器</h3><p>Elasticsearch还附带了可以直接使用的预包装的分析器。接下来我们会列出最重要的分析器。为了证明它们的差异，我们看看每个分析器会从下面的字符串得到哪些词条：</p>
<pre class="line-numbers language-none"><code class="language-none">&quot;Set the shape to semi-transparent by calling set_trans(5)&quot;
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<ul>
<li>标准分析器</li>
</ul>
<p>标准分析器是Elasticsearch默认使用的分析器。它是分析各种语言文本最常用的选择。它根据Unicode 联盟定义的单词边界划分文本。删除绝大部分标点。最后，将词条小写。它会产生：</p>
<pre class="line-numbers language-none"><code class="language-none">set, the, shape, to, semi, transparent, by, calling, set_trans, 5
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<ul>
<li>简单分析器</li>
</ul>
<p>简单分析器在任何不是字母的地方分隔文本，将词条小写。它会产生：</p>
<pre class="line-numbers language-none"><code class="language-none">set, the, shape, to, semi, transparent, by, calling, set, trans
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<ul>
<li>空格分析器</li>
</ul>
<p>空格分析器在空格的地方划分文本。它会产生:</p>
<pre class="line-numbers language-none"><code class="language-none">Set, the, shape, to, semi-transparent, by, calling, set_trans(5)
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<ul>
<li>语言分析器</li>
</ul>
<p>特定语言分析器可用于很多语言。它们可以考虑指定语言的特点。例如，英语分析器附带了一组英语无用词（常用单词，例如and或者the,它们对相关性没有多少影响），它们会被删除。由于理解英语语法的规则，这个分词器可以提取英语单词的词干。</p>
<p>英语分词器会产生下面的词条：</p>
<pre class="line-numbers language-none"><code class="language-none">set, shape, semi, transpar, call, set_tran, 5
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>注意看transparent、calling和set_trans已经变为词根格式。</p>
<h3 id="分析器使用场景"><a href="#分析器使用场景" class="headerlink" title="分析器使用场景"></a>分析器使用场景</h3><p>当我们索引一个文档，它的全文域被分析成词条以用来创建倒排索引。但是，当我们在全文域搜索的时候，我们需要将查询字符串通过相同的分析过程，以保证我们搜索的词条格式与索引中的词条格式一致。</p>
<p>全文查询，理解每个域是如何定义的，因此它们可以做正确的事：</p>
<ul>
<li>当你查询一个全文域时，会对查询字符串应用相同的分析器，以产生正确的搜索词条列表。</li>
<li>当你查询一个精确值域时，不会分析查询字符串，而是搜索你指定的精确值。</li>
</ul>
<h3 id="测试分析器"><a href="#测试分析器" class="headerlink" title="测试分析器"></a>测试分析器</h3><p>有些时候很难理解分词的过程和实际被存储到索引中的词条，特别是你刚接触Elasticsearch。为了理解发生了什么，你可以使用analyze API来看文本是如何被分析的。在消息体里，指定分析器和要分析的文本。</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#GET http<span class="token operator">:</span><span class="token comment">//localhost:9200/_analyze</span>
<span class="token punctuation">&#123;</span>
    <span class="token property">"analyzer"</span><span class="token operator">:</span> <span class="token string">"standard"</span><span class="token punctuation">,</span>
    <span class="token property">"text"</span><span class="token operator">:</span> <span class="token string">"Text to analyze"</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>结果中每个元素代表一个单独的词条：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"tokens"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"text"</span><span class="token punctuation">,</span> 
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> 
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span> 
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"&lt;ALPHANUM>"</span><span class="token punctuation">,</span> 
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">1</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span> 
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"to"</span><span class="token punctuation">,</span> 
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span> 
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">7</span><span class="token punctuation">,</span> 
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"&lt;ALPHANUM>"</span><span class="token punctuation">,</span> 
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">2</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span> 
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"analyze"</span><span class="token punctuation">,</span> 
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">8</span><span class="token punctuation">,</span> 
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">15</span><span class="token punctuation">,</span> 
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"&lt;ALPHANUM>"</span><span class="token punctuation">,</span> 
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">3</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>token是实际存储到索引中的词条。</li>
<li>start_ offset 和end_ offset指明字符在原始字符串中的位置。</li>
<li>position指明词条在原始文本中出现的位置。</li>
</ul>
<h3 id="指定分析器"><a href="#指定分析器" class="headerlink" title="指定分析器"></a>指定分析器</h3><p>当Elasticsearch在你的文档中检测到一个新的字符串域，它会自动设置其为一个全文字符串域，使用标准分析器对它进行分析。你不希望总是这样。可能你想使用一个不同的分析器，适用于你的数据使用的语言。有时候你想要一个字符串域就是一个字符串域，不使用分析，直接索引你传入的精确值，例如用户ID或者一个内部的状态域或标签。要做到这一点，我们必须手动指定这些域的映射。</p>
<p>（细粒度指定分析器）</p>
<h3 id="IK分词器"><a href="#IK分词器" class="headerlink" title="IK分词器"></a>IK分词器</h3><p>首先通过Postman发送GET请求查询分词效果</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"># GET http<span class="token operator">:</span><span class="token comment">//localhost:9200/_analyze</span>
<span class="token punctuation">&#123;</span>
	<span class="token property">"text"</span><span class="token operator">:</span><span class="token string">"测试单词"</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>ES 的默认分词器无法识别中文中测试、单词这样的词汇，而是简单的将每个字拆完分为一个词。</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"tokens"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"测"</span><span class="token punctuation">,</span> 
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> 
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> 
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"&lt;IDEOGRAPHIC>"</span><span class="token punctuation">,</span> 
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">0</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span> 
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"试"</span><span class="token punctuation">,</span> 
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> 
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span> 
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"&lt;IDEOGRAPHIC>"</span><span class="token punctuation">,</span> 
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">1</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span> 
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"单"</span><span class="token punctuation">,</span> 
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span> 
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span> 
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"&lt;IDEOGRAPHIC>"</span><span class="token punctuation">,</span> 
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">2</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span> 
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"词"</span><span class="token punctuation">,</span> 
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span> 
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span> 
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"&lt;IDEOGRAPHIC>"</span><span class="token punctuation">,</span> 
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">3</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>这样的结果显然不符合我们的使用要求，所以我们需要下载ES对应版本的中文分词器。</p>
<p><a target="_blank" rel="noopener" href="https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.8.0">IK中文分词器下载网址</a></p>
<p>将解压后的后的文件夹放入ES根目录下的plugins目录下，重启ES即可使用。</p>
<p>我们这次加入新的查询参数”analyzer”:“ik_max_word”。</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"># GET http<span class="token operator">:</span><span class="token comment">//localhost:9200/_analyze</span>
<span class="token punctuation">&#123;</span>
	<span class="token property">"text"</span><span class="token operator">:</span><span class="token string">"测试单词"</span><span class="token punctuation">,</span>
	<span class="token property">"analyzer"</span><span class="token operator">:</span><span class="token string">"ik_max_word"</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>ik_max_word：会将文本做最细粒度的拆分。</li>
<li>ik_smart：会将文本做最粗粒度的拆分。</li>
</ul>
<p>使用中文分词后的结果为：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"tokens"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"测试"</span><span class="token punctuation">,</span> 
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> 
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span> 
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"CN_WORD"</span><span class="token punctuation">,</span> 
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">0</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span> 
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"单词"</span><span class="token punctuation">,</span> 
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span> 
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span> 
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"CN_WORD"</span><span class="token punctuation">,</span> 
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">1</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>ES中也可以进行扩展词汇，首先查询</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#GET http<span class="token operator">:</span><span class="token comment">//localhost:9200/_analyze</span>

<span class="token punctuation">&#123;</span>
    <span class="token property">"text"</span><span class="token operator">:</span><span class="token string">"弗雷尔卓德"</span><span class="token punctuation">,</span>
    <span class="token property">"analyzer"</span><span class="token operator">:</span><span class="token string">"ik_max_word"</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>仅仅可以得到每个字的分词结果，我们需要做的就是使分词器识别到弗雷尔卓德也是一个词语。</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"tokens"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"弗"</span><span class="token punctuation">,</span>
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"CN_CHAR"</span><span class="token punctuation">,</span>
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">0</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"雷"</span><span class="token punctuation">,</span>
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"CN_CHAR"</span><span class="token punctuation">,</span>
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">1</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"尔"</span><span class="token punctuation">,</span>
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"CN_CHAR"</span><span class="token punctuation">,</span>
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">2</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"卓"</span><span class="token punctuation">,</span>
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"CN_CHAR"</span><span class="token punctuation">,</span>
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">3</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"德"</span><span class="token punctuation">,</span>
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"CN_CHAR"</span><span class="token punctuation">,</span>
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">4</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ol>
<li>首先进入ES根目录中的plugins文件夹下的ik文件夹，进入config目录，创建custom.dic文件，写入“弗雷尔卓德”。</li>
<li>同时打开IKAnalyzer.cfg.xml文件，将新建的custom.dic配置其中。</li>
<li>重启ES服务器。</li>
</ol>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8"?></span>
<span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">properties</span> <span class="token name">SYSTEM</span> <span class="token string">"http://java.sun.com/dtd/properties.dtd"</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>properties</span><span class="token punctuation">></span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>comment</span><span class="token punctuation">></span></span>IK Analyzer扩展配置<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>comment</span><span class="token punctuation">></span></span>
	<span class="token comment">&lt;!--用户可以在这里配置自己的扩展字典 --></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>entry</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>ext_dict<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>custom.dic<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>entry</span><span class="token punctuation">></span></span>
	 <span class="token comment">&lt;!--用户可以在这里配置自己的扩展停止词字典--></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>entry</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>ext_stopwords<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>entry</span><span class="token punctuation">></span></span>
	<span class="token comment">&lt;!--用户可以在这里配置远程扩展字典 --></span>
	<span class="token comment">&lt;!-- &lt;entry key="remote_ext_dict">words_location&lt;/entry> --></span>
	<span class="token comment">&lt;!--用户可以在这里配置远程扩展停止词字典--></span>
	<span class="token comment">&lt;!-- &lt;entry key="remote_ext_stopwords">words_location&lt;/entry> --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>properties</span><span class="token punctuation">></span></span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>扩展后再次查询</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"># GET http<span class="token operator">:</span><span class="token comment">//localhost:9200/_analyze</span>
<span class="token punctuation">&#123;</span>
	<span class="token property">"text"</span><span class="token operator">:</span><span class="token string">"测试单词"</span><span class="token punctuation">,</span>
	<span class="token property">"analyzer"</span><span class="token operator">:</span><span class="token string">"ik_max_word"</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"tokens"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"弗雷尔卓德"</span><span class="token punctuation">,</span>
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"CN_WORD"</span><span class="token punctuation">,</span>
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">0</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="自定义分析器"><a href="#自定义分析器" class="headerlink" title="自定义分析器"></a>自定义分析器</h3><p>虽然Elasticsearch带有一些现成的分析器，然而在分析器上Elasticsearch真正的强大之处在于，你可以通过在一个适合你的特定数据的设置之中组合字符过滤器、分词器、词汇单元过滤器来创建自定义的分析器。在分析与分析器我们说过，一个分析器就是在一个包里面组合了三种函数的一个包装器，三种函数按照顺序被执行：</p>
<h4 id="字符过滤器"><a href="#字符过滤器" class="headerlink" title="字符过滤器"></a>字符过滤器</h4><p>字符过滤器用来整理一个尚未被分词的字符串。例如，如果我们的文本是HTML格式的，它会包含像<code>&lt;p&gt;</code>或者<code>&lt;div&gt;</code>这样的HTML标签，这些标签是我们不想索引的。我们可以使用html清除字符过滤器来移除掉所有的HTML标签，并且像把<code>Á</code>转换为相对应的Unicode字符Á 这样，转换HTML实体。一个分析器可能有0个或者多个字符过滤器。</p>
<h4 id="分词器"><a href="#分词器" class="headerlink" title="分词器"></a>分词器</h4><p>一个分析器必须有一个唯一的分词器。分词器把字符串分解成单个词条或者词汇单元。标准分析器里使用的标准分词器把一个字符串根据单词边界分解成单个词条，并且移除掉大部分的标点符号，然而还有其他不同行为的分词器存在。</p>
<p>例如，关键词分词器完整地输出接收到的同样的字符串，并不做任何分词。空格分词器只根据空格分割文本。正则分词器根据匹配正则表达式来分割文本。</p>
<h4 id="词单元过滤器"><a href="#词单元过滤器" class="headerlink" title="词单元过滤器"></a>词单元过滤器</h4><p>经过分词，作为结果的词单元流会按照指定的顺序通过指定的词单元过滤器。词单元过滤器可以修改、添加或者移除词单元。我们已经提到过lowercase和stop词过滤器，但是在Elasticsearch里面还有很多可供选择的词单元过滤器。词干过滤器把单词遏制为词干。ascii_folding过滤器移除变音符，把一个像”très”这样的词转换为“tres”。</p>
<p>ngram和edge_ngram词单元过滤器可以产生适合用于部分匹配或者自动补全的词单元。</p>
<h4 id="自定义分析器例子"><a href="#自定义分析器例子" class="headerlink" title="自定义分析器例子"></a>自定义分析器例子</h4><p>接下来，我们看看如何创建自定义的分析器：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#PUT http<span class="token operator">:</span><span class="token comment">//localhost:9200/my_index</span>

<span class="token punctuation">&#123;</span>
    <span class="token property">"settings"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"analysis"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
            <span class="token property">"char_filter"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                <span class="token property">"&amp;_to_and"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"mapping"</span><span class="token punctuation">,</span> 
                    <span class="token property">"mappings"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                        <span class="token string">"&amp;=> and "</span>
                    <span class="token punctuation">]</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span> 
            <span class="token property">"filter"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                <span class="token property">"my_stopwords"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"stop"</span><span class="token punctuation">,</span> 
                    <span class="token property">"stopwords"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                        <span class="token string">"the"</span><span class="token punctuation">,</span> 
                        <span class="token string">"a"</span>
                    <span class="token punctuation">]</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span> 
            <span class="token property">"analyzer"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                <span class="token property">"my_analyzer"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
                    <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"custom"</span><span class="token punctuation">,</span> 
                    <span class="token property">"char_filter"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                        <span class="token string">"html_strip"</span><span class="token punctuation">,</span> 
                        <span class="token string">"&amp;_to_and"</span>
                    <span class="token punctuation">]</span><span class="token punctuation">,</span> 
                    <span class="token property">"tokenizer"</span><span class="token operator">:</span> <span class="token string">"standard"</span><span class="token punctuation">,</span> 
                    <span class="token property">"filter"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                        <span class="token string">"lowercase"</span><span class="token punctuation">,</span> 
                        <span class="token string">"my_stopwords"</span>
                    <span class="token punctuation">]</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>索引被创建以后，使用analyze API来测试这个新的分析器：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"># GET http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/my_index/_analyze</span>
<span class="token punctuation">&#123;</span>
    <span class="token property">"text"</span><span class="token operator">:</span><span class="token string">"The quick &amp; brown fox"</span><span class="token punctuation">,</span>
    <span class="token property">"analyzer"</span><span class="token operator">:</span> <span class="token string">"my_analyzer"</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果为：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"tokens"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"quick"</span><span class="token punctuation">,</span>
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">9</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"&lt;ALPHANUM>"</span><span class="token punctuation">,</span>
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">1</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"and"</span><span class="token punctuation">,</span>
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">10</span><span class="token punctuation">,</span>
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">11</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"&lt;ALPHANUM>"</span><span class="token punctuation">,</span>
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">2</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"brown"</span><span class="token punctuation">,</span>
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">12</span><span class="token punctuation">,</span>
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">17</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"&lt;ALPHANUM>"</span><span class="token punctuation">,</span>
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">3</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            <span class="token property">"token"</span><span class="token operator">:</span> <span class="token string">"fox"</span><span class="token punctuation">,</span>
            <span class="token property">"start_offset"</span><span class="token operator">:</span> <span class="token number">18</span><span class="token punctuation">,</span>
            <span class="token property">"end_offset"</span><span class="token operator">:</span> <span class="token number">21</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"&lt;ALPHANUM>"</span><span class="token punctuation">,</span>
            <span class="token property">"position"</span><span class="token operator">:</span> <span class="token number">4</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="47-进阶-文档控制"><a href="#47-进阶-文档控制" class="headerlink" title="47-进阶-文档控制"></a>47-进阶-文档控制</h2><h3 id="文档冲突"><a href="#文档冲突" class="headerlink" title="文档冲突"></a>文档冲突</h3><p>当我们使用index API更新文档，可以一次性读取原始文档，做我们的修改，然后重新索引整个文档。最近的索引请求将获胜：无论最后哪一个文档被索引，都将被唯一存储在Elasticsearch中。如果其他人同时更改这个文档，他们的更改将丢失。</p>
<p>很多时候这是没有问题的。也许我们的主数据存储是一个关系型数据库，我们只是将数据复制到Elasticsearch中并使其可被搜索。也许两个人同时更改相同的文档的几率很小。或者对于我们的业务来说偶尔丢失更改并不是很严重的问题。</p>
<p>但有时丢失了一个变更就是非常严重的。试想我们使用Elasticsearch存储我们网上商城商品库存的数量，每次我们卖一个商品的时候，我们在Elasticsearch中将库存数量减少。有一天，管理层决定做一次促销。突然地，我们一秒要卖好几个商品。假设有两个web程序并行运行，每一个都同时处理所有商品的销售。</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/49ca2ec50db3ddd0fcd1f364ac600b96.png" alt="img"></p>
<p>web_1对stock_count所做的更改已经丢失，因为web_2不知道它的stock_count的拷贝已经过期。结果我们会认为有超过商品的实际数量的库存，因为卖给顾客的库存商品并不存在，我们将让他们非常失望。</p>
<p>变更越频繁，读数据和更新数据的间隙越长，也就越可能丢失变更。在数据库领域中，有两种方法通常被用来确保并发更新时变更不会丢失：</p>
<ul>
<li>悲观并发控制：这种方法被关系型数据库广泛使用，它假定有变更冲突可能发生，因此阻塞访问资源以防止冲突。一个典型的例子是读取一行数据之前先将其锁住，确保只有放置锁的线程能够对这行数据进行修改。</li>
<li>乐观并发控制：Elasticsearch中使用的这种方法假定冲突是不可能发生的，并且不会阻塞正在尝试的操作。然而，如果源数据在读写当中被修改，更新将会失败。应用程序接下来将决定该如何解决冲突。例如，可以重试更新、使用新的数据、或者将相关情况报告给用户。</li>
</ul>
<h3 id="乐观并发控制"><a href="#乐观并发控制" class="headerlink" title="乐观并发控制"></a>乐观并发控制</h3><p>Elasticsearch是分布式的。当文档创建、更新或删除时，新版本的文档必须复制到集群中的其他节点。Elasticsearch也是异步和并发的，这意味着这些复制请求被并行发送，并且到达目的地时也许顺序是乱的。Elasticsearch需要一种方法确保文档的旧版本不会覆盖新的版本。</p>
<p>当我们之前讨论index,GET和DELETE请求时，我们指出每个文档都有一个_version（版本号），当文档被修改时版本号递增。Elasticsearch使用这个version号来确保变更以正确顺序得到执行。如果旧版本的文档在新版本之后到达，它可以被简单的忽略。</p>
<p>我们可以利用version号来确保应用中相互冲突的变更不会导致数据丢失。我们通过指定想要修改文档的version号来达到这个目的。如果该版本不是当前版本号，我们的请求将会失败。</p>
<p>老的版本es使用version，但是新版本不支持了，会报下面的错误，提示我们用if_seq_no和if_primary_term</p>
<p>创建索引</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#PUT http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/shopping/_create/1001</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>返回结果</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1001"</span><span class="token punctuation">,</span>
    <span class="token property">"_version"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">"result"</span><span class="token operator">:</span> <span class="token string">"created"</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"_seq_no"</span><span class="token operator">:</span> <span class="token number">10</span><span class="token punctuation">,</span>
    <span class="token property">"_primary_term"</span><span class="token operator">:</span> <span class="token number">15</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>更新数据</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#POST http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/shopping/_update/1001</span>
<span class="token punctuation">&#123;</span>
    <span class="token property">"doc"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
        <span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"华为手机"</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1001"</span><span class="token punctuation">,</span>
    <span class="token property">"_version"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
    <span class="token property">"result"</span><span class="token operator">:</span> <span class="token string">"updated"</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"_seq_no"</span><span class="token operator">:</span> <span class="token number">11</span><span class="token punctuation">,</span>
    <span class="token property">"_primary_term"</span><span class="token operator">:</span> <span class="token number">15</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>旧版本使用的防止冲突更新方法：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#POST http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/shopping/_update/1001?version=1</span>
<span class="token punctuation">&#123;</span>
    <span class="token property">"doc"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
        <span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"华为手机2"</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"error"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"root_cause"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">&#123;</span>
                <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"action_request_validation_exception"</span><span class="token punctuation">,</span>
                <span class="token property">"reason"</span><span class="token operator">:</span> <span class="token string">"Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;"</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"action_request_validation_exception"</span><span class="token punctuation">,</span>
        <span class="token property">"reason"</span><span class="token operator">:</span> <span class="token string">"Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;"</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"status"</span><span class="token operator">:</span> <span class="token number">400</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>新版本使用的防止冲突更新方法：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#POST http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/shopping/_update/1001?if_seq_no=11&amp;if_primary_term=15</span>
<span class="token punctuation">&#123;</span>
    <span class="token property">"doc"</span><span class="token operator">:</span><span class="token punctuation">&#123;</span>
        <span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"华为手机2"</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1001"</span><span class="token punctuation">,</span>
    <span class="token property">"_version"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
    <span class="token property">"result"</span><span class="token operator">:</span> <span class="token string">"updated"</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"_seq_no"</span><span class="token operator">:</span> <span class="token number">12</span><span class="token punctuation">,</span>
    <span class="token property">"_primary_term"</span><span class="token operator">:</span> <span class="token number">16</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="外部系统版本控制"><a href="#外部系统版本控制" class="headerlink" title="外部系统版本控制"></a>外部系统版本控制</h3><p>一个常见的设置是使用其它数据库作为主要的数据存储，使用Elasticsearch做数据检索，这意味着主数据库的所有更改发生时都需要被复制到Elasticsearch，如果多个进程负责这一数据同步，你可能遇到类似于之前描述的并发问题。</p>
<p>如果你的主数据库已经有了版本号，或一个能作为版本号的字段值比如timestamp，那么你就可以在Elasticsearch中通过增加version_type&#x3D;extermal到查询字符串的方式重用这些相同的版本号，版本号必须是大于零的整数，且小于9.2E+18，一个Java中long类型的正值。</p>
<p>外部版本号的处理方式和我们之前讨论的内部版本号的处理方式有些不同，Elasticsearch不是检查当前_version和请求中指定的版本号是否相同，而是检查当前_version是否小于指定的版本号。如果请求成功，外部的版本号作为文档的新_version进行存储。</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#POST http<span class="token operator">:</span><span class="token comment">//127.0.0.1:9200/shopping/_doc/1001?version=300&amp;version_type=external</span>
<span class="token punctuation">&#123;</span>
	<span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"华为手机2"</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>返回结果：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
    <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span>
    <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1001"</span><span class="token punctuation">,</span>
    <span class="token property">"_version"</span><span class="token operator">:</span> <span class="token number">300</span><span class="token punctuation">,</span>
    <span class="token property">"result"</span><span class="token operator">:</span> <span class="token string">"updated"</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token property">"_seq_no"</span><span class="token operator">:</span> <span class="token number">13</span><span class="token punctuation">,</span>
    <span class="token property">"_primary_term"</span><span class="token operator">:</span> <span class="token number">16</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="48-进阶-文档展示-Kibana"><a href="#48-进阶-文档展示-Kibana" class="headerlink" title="48-进阶-文档展示-Kibana"></a>48-进阶-文档展示-Kibana</h2><p>Kibana是一个免费且开放的用户界面，能够让你对Elasticsearch数据进行可视化，并让你在Elastic Stack中进行导航。你可以进行各种操作，从跟踪查询负载，到理解请求如何流经你的整个应用，都能轻松完成。</p>
<p><a target="_blank" rel="noopener" href="https://artifacts.elastic.co/downloads/kibana/kibana-7.8.0-windows-x86_64.zip">Kibana下载网址</a></p>
<p>一、解压缩下载的zip文件。</p>
<p>二、修改config&#x2F;kibana.yml文件。</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token comment"># 默认端口</span>
<span class="token key atrule">server.port</span><span class="token punctuation">:</span> <span class="token number">5601</span>
<span class="token comment"># ES服务器的地址</span>
<span class="token key atrule">elasticsearch.hosts</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"http://localhost:9200"</span><span class="token punctuation">]</span>
<span class="token comment"># 索引名</span>
<span class="token key atrule">kibana.index</span><span class="token punctuation">:</span> <span class="token string">".kibana"</span>
<span class="token comment"># 支持中文</span>
<span class="token key atrule">i18n.locale</span><span class="token punctuation">:</span> <span class="token string">"zh-CN"</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>三、Windows环境下执行bin&#x2F;kibana.bat文件。（首次启动有点耗时）</p>
<p>四、通过浏览器访问：<a href="http://localhost:5601。">http://localhost:5601。</a></p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/d066ba5e26916624b5d056d04c5580ac.png" alt="img"></p>
<h1 id="第5章-Elasticsearch集成"><a href="#第5章-Elasticsearch集成" class="headerlink" title="第5章 Elasticsearch集成"></a>第5章 Elasticsearch集成</h1><h2 id="49-框架集成-SpringData-整体介绍"><a href="#49-框架集成-SpringData-整体介绍" class="headerlink" title="49-框架集成-SpringData-整体介绍"></a>49-框架集成-SpringData-整体介绍</h2><p>Spring Data是一个用于简化数据库、非关系型数据库、索引库访问，并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷，并支持map-reduce框架和云计算数据服务。Spring Data可以极大的简化JPA(Elasticsearch…)的写法，可以在几乎不用写实现的情况下，实现对数据的访问和操作。除了CRUD外，还包括如分页、排序等一些常用的功能。</p>
<p><a target="_blank" rel="noopener" href="https://spring.io/projects/spring-data">Spring Data的官网</a></p>
<p>Spring Data常用的功能模块如下：</p>
<ul>
<li>Spring Data JDBC</li>
<li>Spring Data JPA</li>
<li>Spring Data LDAP</li>
<li>Spring Data MongoDB</li>
<li>Spring Data Redis</li>
<li>Spring Data R2DBC</li>
<li>Spring Data REST</li>
<li>Spring Data for Apache Cassandra</li>
<li>Spring Data for Apache Geode</li>
<li>Spring Data for Apache Solr</li>
<li>Spring Data for Pivotal GemFire</li>
<li>Spring Data Couchbase</li>
<li>Spring Data Elasticsearch</li>
<li>Spring Data Envers</li>
<li>Spring Data Neo4j</li>
<li>Spring Data JDBC Extensions</li>
<li>Spring for Apache Hadoop</li>
</ul>
<h3 id="Spring-Data-Elasticsearch介绍"><a href="#Spring-Data-Elasticsearch介绍" class="headerlink" title="Spring Data Elasticsearch介绍"></a>Spring Data Elasticsearch介绍</h3><p>Spring Data Elasticsearch基于Spring Data API简化Elasticsearch操作，将原始操作Elasticsearch的客户端API进行封装。Spring Data为Elasticsearch项目提供集成搜索引擎。Spring Data Elasticsearch POJO的关键功能区域为中心的模型与Elastichsearch交互文档和轻松地编写一个存储索引库数据访问层。</p>
<p><a target="_blank" rel="noopener" href="https://spring.io/projects/spring-data-elasticsearch">Spring Data Elasticsearch官网</a></p>
<h2 id="50-框架集成-SpringData-代码功能集成"><a href="#50-框架集成-SpringData-代码功能集成" class="headerlink" title="50-框架集成-SpringData-代码功能集成"></a>50-框架集成-SpringData-代码功能集成</h2><p>一、创建Maven项目。</p>
<p>二、修改pom文件，增加依赖关系。</p>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8"?></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">"</span></span>
         <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span>
         <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">></span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.3.6.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>relativePath</span><span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.lun<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>SpringDataWithES<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.0.0-SNAPSHOT<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>properties</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>maven.compiler.source</span><span class="token punctuation">></span></span>8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>maven.compiler.source</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>maven.compiler.target</span><span class="token punctuation">></span></span>8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>maven.compiler.target</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>properties</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.projectlombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>lombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-data-elasticsearch<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-devtools<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">></span></span>runtime<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>optional</span><span class="token punctuation">></span></span>true<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>optional</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">></span></span>test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">></span></span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>三、增加配置文件。</p>
<p>在resources目录中增加application.properties文件</p>
<pre class="line-numbers language-properties" data-language="properties"><code class="language-properties"><span class="token comment"># es 服务地址</span>
<span class="token key attr-name">elasticsearch.host</span><span class="token punctuation">=</span><span class="token value attr-value">127.0.0.1</span>
<span class="token comment"># es 服务端口</span>
<span class="token key attr-name">elasticsearch.port</span><span class="token punctuation">=</span><span class="token value attr-value">9200</span>
<span class="token comment"># 配置日志级别,开启 debug 日志</span>
<span class="token key attr-name">logging.level.com.atguigu.es</span><span class="token punctuation">=</span><span class="token value attr-value">debug</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>四、Spring Boot主程序。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span></span><span class="token class-name">SpringApplication</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>autoconfigure<span class="token punctuation">.</span></span><span class="token class-name">SpringBootApplication</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MainApplication</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">MainApplication</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>五、数据实体类。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">AllArgsConstructor</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">Data</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">NoArgsConstructor</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">ToString</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Id</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>annotations<span class="token punctuation">.</span></span><span class="token class-name">Document</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>annotations<span class="token punctuation">.</span></span><span class="token class-name">Field</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>annotations<span class="token punctuation">.</span></span><span class="token class-name">FieldType</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@Data</span>
<span class="token annotation punctuation">@NoArgsConstructor</span>
<span class="token annotation punctuation">@AllArgsConstructor</span>
<span class="token annotation punctuation">@ToString</span>
<span class="token annotation punctuation">@Document</span><span class="token punctuation">(</span>indexName <span class="token operator">=</span> <span class="token string">"shopping"</span><span class="token punctuation">,</span> shards <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">,</span> replicas <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Product</span> <span class="token punctuation">&#123;</span>
    <span class="token comment">//必须有 id,这里的 id 是全局唯一的标识，等同于 es 中的"_id"</span>
    <span class="token annotation punctuation">@Id</span>
    <span class="token keyword">private</span> <span class="token class-name">Long</span> id<span class="token punctuation">;</span><span class="token comment">//商品唯一标识</span>

    <span class="token comment">/**
     * type : 字段数据类型
     * analyzer : 分词器类型
     * index : 是否索引(默认:true)
     * Keyword : 短语,不进行分词
     */</span>
    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Text</span><span class="token punctuation">,</span> analyzer <span class="token operator">=</span> <span class="token string">"ik_max_word"</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> title<span class="token punctuation">;</span><span class="token comment">//商品名称</span>

    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Keyword</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> category<span class="token punctuation">;</span><span class="token comment">//分类名称</span>

    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Double</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">Double</span> price<span class="token punctuation">;</span><span class="token comment">//商品价格</span>

    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Keyword</span><span class="token punctuation">,</span> index <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> images<span class="token punctuation">;</span><span class="token comment">//图片地址</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>六、配置类</p>
<ul>
<li>ElasticsearchRestTemplate是spring-data-elasticsearch项目中的一个类,和其他spring项目中的template类似。</li>
<li>在新版的spring-data-elasticsearch中，ElasticsearchRestTemplate代替了原来的ElasticsearchTemplate。</li>
<li>原因是ElasticsearchTemplate基于TransportClient，TransportClient即将在8.x以后的版本中移除。所以，我们推荐使用ElasticsearchRestTemplate。</li>
<li>ElasticsearchRestTemplate基于RestHighLevelClient客户端的。需要自定义配置类，继承AbstractElasticsearchConfiguration，并实现elasticsearchClient()抽象方法，创建RestHighLevelClient对象。</li>
</ul>
<p>AbstractElasticsearchConfiguration源码：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">package org.springframework.data.elasticsearch.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;

<span class="token comment">/**
 * @author Christoph Strobl
 * @author Peter-Josef Meisch
 * @since 3.2
 * @see ElasticsearchConfigurationSupport
 */</span>
public abstract class AbstractElasticsearchConfiguration extends ElasticsearchConfigurationSupport <span class="token punctuation">&#123;</span>

	<span class="token comment">//需重写本方法</span>
	public abstract RestHighLevelClient elasticsearchClient();

	@Bean(name = <span class="token punctuation">&#123;</span> <span class="token string">"elasticsearchOperations"</span><span class="token punctuation">,</span> <span class="token string">"elasticsearchTemplate"</span> <span class="token punctuation">&#125;</span>)
	public ElasticsearchOperations elasticsearchOperations(ElasticsearchConverter elasticsearchConverter) <span class="token punctuation">&#123;</span>
		return new ElasticsearchRestTemplate(elasticsearchClient()<span class="token punctuation">,</span> elasticsearchConverter);
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>需要自定义配置类，继承AbstractElasticsearchConfiguration，并实现elasticsearchClient()抽象方法，创建RestHighLevelClient对象。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">Data</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpHost</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestClient</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestClientBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestHighLevelClient</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>context<span class="token punctuation">.</span>properties<span class="token punctuation">.</span></span><span class="token class-name">ConfigurationProperties</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Configuration</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>config<span class="token punctuation">.</span></span><span class="token class-name">AbstractElasticsearchConfiguration</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@ConfigurationProperties</span><span class="token punctuation">(</span>prefix <span class="token operator">=</span> <span class="token string">"elasticsearch"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@Configuration</span>
<span class="token annotation punctuation">@Data</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ElasticsearchConfig</span> <span class="token keyword">extends</span> <span class="token class-name">AbstractElasticsearchConfiguration</span><span class="token punctuation">&#123;</span>

    <span class="token keyword">private</span> <span class="token class-name">String</span> host <span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> port <span class="token punctuation">;</span>
    <span class="token comment">//重写父类方法</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">RestHighLevelClient</span> <span class="token function">elasticsearchClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">RestClientBuilder</span> builder <span class="token operator">=</span> <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span>host<span class="token punctuation">,</span> port<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">RestHighLevelClient</span> restHighLevelClient <span class="token operator">=</span> <span class="token keyword">new</span>
                <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>builder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> restHighLevelClient<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>七、DAO 数据访问对象</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>model<span class="token punctuation">.</span></span><span class="token class-name">Product</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>repository<span class="token punctuation">.</span></span><span class="token class-name">ElasticsearchRepository</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span></span><span class="token class-name">Repository</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@Repository</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ProductDao</span> <span class="token keyword">extends</span> <span class="token class-name">ElasticsearchRepository</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">,</span> <span class="token class-name">Long</span><span class="token punctuation">></span></span><span class="token punctuation">&#123;</span>

<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="51-框架集成-SpringData-集成测试-索引操作"><a href="#51-框架集成-SpringData-集成测试-索引操作" class="headerlink" title="51-框架集成-SpringData-集成测试-索引操作"></a>51-框架集成-SpringData-集成测试-索引操作</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>model<span class="token punctuation">.</span></span><span class="token class-name">Product</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span></span><span class="token class-name">Test</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runner<span class="token punctuation">.</span></span><span class="token class-name">RunWith</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>beans<span class="token punctuation">.</span>factory<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Autowired</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>test<span class="token punctuation">.</span>context<span class="token punctuation">.</span></span><span class="token class-name">SpringBootTest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>core<span class="token punctuation">.</span></span><span class="token class-name">ElasticsearchRestTemplate</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>test<span class="token punctuation">.</span>context<span class="token punctuation">.</span>junit4<span class="token punctuation">.</span></span><span class="token class-name">SpringRunner</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@RunWith</span><span class="token punctuation">(</span><span class="token class-name">SpringRunner</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@SpringBootTest</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SpringDataESIndexTest</span> <span class="token punctuation">&#123;</span>
    <span class="token comment">//注入 ElasticsearchRestTemplate</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">ElasticsearchRestTemplate</span> elasticsearchRestTemplate<span class="token punctuation">;</span>
    <span class="token comment">//创建索引并增加映射配置</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//创建索引，系统初始化会自动创建索引</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"创建索引"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">deleteIndex</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//创建索引，系统初始化会自动创建索引</span>
        <span class="token keyword">boolean</span> flg <span class="token operator">=</span> elasticsearchRestTemplate<span class="token punctuation">.</span><span class="token function">deleteIndex</span><span class="token punctuation">(</span><span class="token class-name">Product</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"删除索引 = "</span> <span class="token operator">+</span> flg<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>用Postman检测有没有创建和删除。</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#GET http<span class="token operator">:</span><span class="token comment">//localhost:9200/_cat/indices?v </span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h2 id="52-框架集成-SpringData-集成测试-文档操作"><a href="#52-框架集成-SpringData-集成测试-文档操作" class="headerlink" title="52-框架集成-SpringData-集成测试-文档操作"></a>52-框架集成-SpringData-集成测试-文档操作</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>dao<span class="token punctuation">.</span></span><span class="token class-name">ProductDao</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>model<span class="token punctuation">.</span></span><span class="token class-name">Product</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span></span><span class="token class-name">Test</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runner<span class="token punctuation">.</span></span><span class="token class-name">RunWith</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>beans<span class="token punctuation">.</span>factory<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Autowired</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>test<span class="token punctuation">.</span>context<span class="token punctuation">.</span></span><span class="token class-name">SpringBootTest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>domain<span class="token punctuation">.</span></span><span class="token class-name">Page</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>domain<span class="token punctuation">.</span></span><span class="token class-name">PageRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>domain<span class="token punctuation">.</span></span><span class="token class-name">Sort</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>test<span class="token punctuation">.</span>context<span class="token punctuation">.</span>junit4<span class="token punctuation">.</span></span><span class="token class-name">SpringRunner</span></span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">ArrayList</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">List</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@RunWith</span><span class="token punctuation">(</span><span class="token class-name">SpringRunner</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@SpringBootTest</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SpringDataESProductDaoTest</span> <span class="token punctuation">&#123;</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">ProductDao</span> productDao<span class="token punctuation">;</span>
    <span class="token comment">/**
     * 新增
     */</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">Product</span> product <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Product</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span><span class="token number">2L</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setTitle</span><span class="token punctuation">(</span><span class="token string">"华为手机"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setCategory</span><span class="token punctuation">(</span><span class="token string">"手机"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setPrice</span><span class="token punctuation">(</span><span class="token number">2999.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setImages</span><span class="token punctuation">(</span><span class="token string">"http://www.atguigu/hw.jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        productDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>product<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token comment">//POSTMAN, GET http://localhost:9200/product/_doc/2</span>

    <span class="token comment">//修改</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">Product</span> product <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Product</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span><span class="token number">2L</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setTitle</span><span class="token punctuation">(</span><span class="token string">"小米 2 手机"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setCategory</span><span class="token punctuation">(</span><span class="token string">"手机"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setPrice</span><span class="token punctuation">(</span><span class="token number">9999.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setImages</span><span class="token punctuation">(</span><span class="token string">"http://www.atguigu/xm.jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        productDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>product<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token comment">//POSTMAN, GET http://localhost:9200/product/_doc/2</span>


    <span class="token comment">//根据 id 查询</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">findById</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">Product</span> product <span class="token operator">=</span> productDao<span class="token punctuation">.</span><span class="token function">findById</span><span class="token punctuation">(</span><span class="token number">2L</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>product<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">Iterable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">></span></span> products <span class="token operator">=</span> productDao<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Product</span> product <span class="token operator">:</span> products<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>product<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token comment">//删除</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">Product</span> product <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Product</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span><span class="token number">2L</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        productDao<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>product<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token comment">//POSTMAN, GET http://localhost:9200/product/_doc/2</span>

    <span class="token comment">//批量新增</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">saveAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">></span></span> productList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token class-name">Product</span> product <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Product</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            product<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span><span class="token class-name">Long</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            product<span class="token punctuation">.</span><span class="token function">setTitle</span><span class="token punctuation">(</span><span class="token string">"["</span><span class="token operator">+</span>i<span class="token operator">+</span><span class="token string">"]小米手机"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            product<span class="token punctuation">.</span><span class="token function">setCategory</span><span class="token punctuation">(</span><span class="token string">"手机"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            product<span class="token punctuation">.</span><span class="token function">setPrice</span><span class="token punctuation">(</span><span class="token number">1999.0</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
            product<span class="token punctuation">.</span><span class="token function">setImages</span><span class="token punctuation">(</span><span class="token string">"http://www.atguigu/xm.jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            productList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>product<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        productDao<span class="token punctuation">.</span><span class="token function">saveAll</span><span class="token punctuation">(</span>productList<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token comment">//分页查询</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">findByPageable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//设置排序(排序方式，正序还是倒序，排序的id)</span>
        <span class="token class-name">Sort</span> sort <span class="token operator">=</span> <span class="token class-name">Sort</span><span class="token punctuation">.</span><span class="token function">by</span><span class="token punctuation">(</span><span class="token class-name">Sort<span class="token punctuation">.</span>Direction</span><span class="token punctuation">.</span><span class="token constant">DESC</span><span class="token punctuation">,</span><span class="token string">"id"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> currentPage<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//当前页，第一页从0开始，1表示第二页</span>
        <span class="token keyword">int</span> pageSize <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span><span class="token comment">//每页显示多少条</span>
        <span class="token comment">//设置查询分页</span>
        <span class="token class-name">PageRequest</span> pageRequest <span class="token operator">=</span> <span class="token class-name">PageRequest</span><span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span>currentPage<span class="token punctuation">,</span> pageSize<span class="token punctuation">,</span>sort<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//分页查询</span>
        <span class="token class-name">Page</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">></span></span> productPage <span class="token operator">=</span> productDao<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span>pageRequest<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Product</span> <span class="token class-name">Product</span> <span class="token operator">:</span> productPage<span class="token punctuation">.</span><span class="token function">getContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Product</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="53-框架集成-SpringData-集成测试-文档搜索"><a href="#53-框架集成-SpringData-集成测试-文档搜索" class="headerlink" title="53-框架集成-SpringData-集成测试-文档搜索"></a>53-框架集成-SpringData-集成测试-文档搜索</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>dao<span class="token punctuation">.</span></span><span class="token class-name">ProductDao</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>lun<span class="token punctuation">.</span>model<span class="token punctuation">.</span></span><span class="token class-name">Product</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">QueryBuilders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>index<span class="token punctuation">.</span>query<span class="token punctuation">.</span></span><span class="token class-name">TermQueryBuilder</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span></span><span class="token class-name">Test</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runner<span class="token punctuation">.</span></span><span class="token class-name">RunWith</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>beans<span class="token punctuation">.</span>factory<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Autowired</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>test<span class="token punctuation">.</span>context<span class="token punctuation">.</span></span><span class="token class-name">SpringBootTest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>domain<span class="token punctuation">.</span></span><span class="token class-name">PageRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>test<span class="token punctuation">.</span>context<span class="token punctuation">.</span>junit4<span class="token punctuation">.</span></span><span class="token class-name">SpringRunner</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@RunWith</span><span class="token punctuation">(</span><span class="token class-name">SpringRunner</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@SpringBootTest</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SpringDataESSearchTest</span> <span class="token punctuation">&#123;</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">ProductDao</span> productDao<span class="token punctuation">;</span>
    <span class="token comment">/**
     * term 查询
     * search(termQueryBuilder) 调用搜索方法，参数查询构建器对象
     */</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">termQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">TermQueryBuilder</span> termQueryBuilder <span class="token operator">=</span> <span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">termQuery</span><span class="token punctuation">(</span><span class="token string">"title"</span><span class="token punctuation">,</span> <span class="token string">"小米"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">Iterable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">></span></span> products <span class="token operator">=</span> productDao<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>termQueryBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Product</span> product <span class="token operator">:</span> products<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>product<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token comment">/**
     * term 查询加分页
     */</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">termQueryByPage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token keyword">int</span> currentPage<span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span>
        <span class="token keyword">int</span> pageSize <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
        <span class="token comment">//设置查询分页</span>
        <span class="token class-name">PageRequest</span> pageRequest <span class="token operator">=</span> <span class="token class-name">PageRequest</span><span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span>currentPage<span class="token punctuation">,</span> pageSize<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">TermQueryBuilder</span> termQueryBuilder <span class="token operator">=</span> <span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">termQuery</span><span class="token punctuation">(</span><span class="token string">"title"</span><span class="token punctuation">,</span> <span class="token string">"小米"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">Iterable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">></span></span> products <span class="token operator">=</span>
                        productDao<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>termQueryBuilder<span class="token punctuation">,</span>pageRequest<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Product</span> product <span class="token operator">:</span> products<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>product<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="54-框架集成-SparkStreaming-集成"><a href="#54-框架集成-SparkStreaming-集成" class="headerlink" title="54-框架集成-SparkStreaming-集成"></a>54-框架集成-SparkStreaming-集成</h2><p>Spark Streaming是Spark core API的扩展，支持实时数据流的处理，并且具有可扩展，高吞吐量，容错的特点。数据可以从许多来源获取,如Kafka，Flume，Kinesis或TCP sockets，并且可以使用复杂的算法进行处理，这些算法使用诸如map，reduce，join和window等高级函数表示。最后，处理后的数据可以推送到文件系统，数据库等。实际上，您可以将Spark的机器学习和图形处理算法应用于数据流。</p>
<p>一、创建Maven项目。</p>
<p>二、修改pom文件，增加依赖关系。</p>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token prolog">&lt;?xml version="1.0" encoding="utf-8"?></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span>
    <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">"</span></span>
    <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">></span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.lun.es<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>sparkstreaming-elasticsearch<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>properties</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>maven.compiler.source</span><span class="token punctuation">></span></span>8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>maven.compiler.source</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>maven.compiler.target</span><span class="token punctuation">></span></span>8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>maven.compiler.target</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>properties</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.spark<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spark-core_2.12<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>3.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.spark<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spark-streaming_2.12<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>3.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.elasticsearch<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>elasticsearch<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>7.8.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token comment">&lt;!-- elasticsearch 的客户端 --></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.elasticsearch.client<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>elasticsearch-rest-high-level-client<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>7.8.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token comment">&lt;!-- elasticsearch 依赖 2.x 的 log4j --></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.logging.log4j<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>log4j-api<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.8.2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.logging.log4j<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>log4j-core<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.8.2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token comment">&lt;!-- &lt;dependency>--></span>
        <span class="token comment">&lt;!-- &lt;groupId>com.fasterxml.jackson.core&lt;/groupId>--></span>
        <span class="token comment">&lt;!-- &lt;artifactId>jackson-databind&lt;/artifactId>--></span>
        <span class="token comment">&lt;!-- &lt;version>2.11.1&lt;/version>--></span>
        <span class="token comment">&lt;!-- &lt;/dependency>--></span>
        <span class="token comment">&lt;!-- &amp;lt;!&amp;ndash; junit 单元测试 &amp;ndash;&amp;gt;--></span>
        <span class="token comment">&lt;!-- &lt;dependency>--></span>
        <span class="token comment">&lt;!-- &lt;groupId>junit&lt;/groupId>--></span>
        <span class="token comment">&lt;!-- &lt;artifactId>junit&lt;/artifactId>--></span>
        <span class="token comment">&lt;!-- &lt;version>4.12&lt;/version>--></span>
        <span class="token comment">&lt;!-- &lt;/dependency>--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">></span></span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>三、功能实现</p>
<pre class="line-numbers language-scala" data-language="scala"><code class="language-scala"><span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span>HttpHost
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>spark<span class="token punctuation">.</span></span>SparkConf
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>spark<span class="token punctuation">.</span>streaming<span class="token punctuation">.</span>dstream<span class="token punctuation">.</span></span>ReceiverInputDStream
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>spark<span class="token punctuation">.</span>streaming<span class="token punctuation">.</span></span><span class="token punctuation">&#123;</span>Seconds<span class="token punctuation">,</span> StreamingContext<span class="token punctuation">&#125;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>index<span class="token punctuation">.</span></span>IndexRequest
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span>indices<span class="token punctuation">.</span></span>CreateIndexRequest
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token punctuation">&#123;</span>RequestOptions<span class="token punctuation">,</span> RestClient<span class="token punctuation">,</span> RestHighLevelClient<span class="token punctuation">&#125;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>common<span class="token punctuation">.</span>xcontent<span class="token punctuation">.</span></span>XContentType
<span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span>Date

<span class="token keyword">object</span> SparkStreamingESTest <span class="token punctuation">&#123;</span>

    <span class="token keyword">def</span> main<span class="token punctuation">(</span>args<span class="token operator">:</span> Array<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Unit</span> <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">val</span> sparkConf <span class="token operator">=</span> <span class="token keyword">new</span> SparkConf<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>setMaster<span class="token punctuation">(</span><span class="token string">"local[*]"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>setAppName<span class="token punctuation">(</span><span class="token string">"ESTest"</span><span class="token punctuation">)</span>
        <span class="token keyword">val</span> ssc <span class="token operator">=</span> <span class="token keyword">new</span> StreamingContext<span class="token punctuation">(</span>sparkConf<span class="token punctuation">,</span> Seconds<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">val</span> ds<span class="token operator">:</span> ReceiverInputDStream<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span> <span class="token operator">=</span> ssc<span class="token punctuation">.</span>socketTextStream<span class="token punctuation">(</span><span class="token string">"localhost"</span><span class="token punctuation">,</span> <span class="token number">9999</span><span class="token punctuation">)</span>
        ds<span class="token punctuation">.</span>foreachRDD<span class="token punctuation">(</span>
            rdd <span class="token keyword">=></span> <span class="token punctuation">&#123;</span>
                println<span class="token punctuation">(</span><span class="token string">"*************** "</span> <span class="token operator">+</span> <span class="token keyword">new</span> Date<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                rdd<span class="token punctuation">.</span>foreach<span class="token punctuation">(</span>
                    data <span class="token keyword">=></span> <span class="token punctuation">&#123;</span>
                        <span class="token keyword">val</span> client <span class="token operator">=</span> <span class="token keyword">new</span> RestHighLevelClient<span class="token punctuation">(</span>RestClient<span class="token punctuation">.</span>builder<span class="token punctuation">(</span><span class="token keyword">new</span> HttpHost<span class="token punctuation">(</span><span class="token string">"localhost"</span><span class="token punctuation">,</span> <span class="token number">9200</span><span class="token punctuation">,</span> <span class="token string">"http"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token comment">// 新增文档 - 请求对象</span>
                        <span class="token keyword">val</span> request <span class="token operator">=</span> <span class="token keyword">new</span> IndexRequest<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        
                        <span class="token comment">// 设置索引及唯一性标识</span>
                        <span class="token keyword">val</span> ss <span class="token operator">=</span> data<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">" "</span><span class="token punctuation">)</span>
                        println<span class="token punctuation">(</span><span class="token string">"ss = "</span> <span class="token operator">+</span> ss<span class="token punctuation">.</span>mkString<span class="token punctuation">(</span><span class="token string">","</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                        request<span class="token punctuation">.</span>index<span class="token punctuation">(</span><span class="token string">"sparkstreaming"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>id<span class="token punctuation">(</span>ss<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        
                        <span class="token keyword">val</span> productJson <span class="token operator">=</span>
                            <span class="token string-interpolation"><span class="token id function">s</span><span class="token string">"""
                            | &#123; "data":"</span><span class="token interpolation"><span class="token punctuation">$&#123;</span><span class="token expression">ss<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span></span><span class="token punctuation">&#125;</span></span><span class="token string">" &#125;
                            |"""</span></span><span class="token punctuation">.</span>stripMargin<span class="token punctuation">;</span>
                        
                        <span class="token comment">// 添加文档数据，数据格式为 JSON 格式</span>
                        request<span class="token punctuation">.</span>source<span class="token punctuation">(</span>productJson<span class="token punctuation">,</span>XContentType<span class="token punctuation">.</span>JSON<span class="token punctuation">)</span><span class="token punctuation">;</span>
                        
                        <span class="token comment">// 客户端发送请求，获取响应对象</span>
                        <span class="token keyword">val</span> response <span class="token operator">=</span> client<span class="token punctuation">.</span>index<span class="token punctuation">(</span>request<span class="token punctuation">,</span>
                        RequestOptions<span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span>println<span class="token punctuation">(</span><span class="token string">"_index:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span>getIndex<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span>println<span class="token punctuation">(</span><span class="token string">"_id:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span>getId<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span>println<span class="token punctuation">(</span><span class="token string">"_result:"</span> <span class="token operator">+</span> response<span class="token punctuation">.</span>getResult<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        client<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
                    <span class="token punctuation">&#125;</span>
                <span class="token punctuation">)</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">)</span>
        ssc<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
        ssc<span class="token punctuation">.</span>awaitTermination<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="55-框架集成-Flink-集成"><a href="#55-框架集成-Flink-集成" class="headerlink" title="55-框架集成-Flink-集成"></a>55-框架集成-Flink-集成</h2><p>Apache Spark是一-种基于内存的快速、通用、可扩展的大数据分析计算引擎。Apache Spark掀开了内存计算的先河，以内存作为赌注，贏得了内存计算的飞速发展。但是在其火热的同时，开发人员发现，在Spark中，计算框架普遍存在的缺点和不足依然没有完全解决，而这些问题随着5G时代的来临以及决策者对实时数据分析结果的迫切需要而凸显的更加明显：</p>
<ul>
<li>乱序数据，迟到数据</li>
<li>低延迟，高吞吐，准确性</li>
<li>容错性</li>
<li>数据精准一次性处理（Exactly-Once）</li>
</ul>
<p>Apache Flink是一个框架和分布式处理引擎，用于对无界和有界数据流进行有状态计算。在Spark火热的同时，也默默地发展自己，并尝试着解决其他计算框架的问题。慢慢地，随着这些问题的解决，Flink慢慢被绝大数程序员所熟知并进行大力推广，阿里公司在2015年改进Flink，并创建了内部分支Blink，目前服务于阿里集团内部搜索、推荐、广告和蚂蚁等大量核心实时业务。</p>
<p>一、创建Maven项目。</p>
<p>二、修改pom文件，增加相关依赖类库。</p>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8"?></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span>
    <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">"</span></span>
    <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span>
<span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">></span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.lun.es<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>flink-elasticsearch<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>properties</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>maven.compiler.source</span><span class="token punctuation">></span></span>8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>maven.compiler.source</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>maven.compiler.target</span><span class="token punctuation">></span></span>8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>maven.compiler.target</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>properties</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.flink<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>flink-scala_2.12<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.12.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.flink<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>flink-streaming-scala_2.12<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.12.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.flink<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>flink-clients_2.12<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.12.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.flink<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>flink-connector-elasticsearch7_2.11<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.12.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token comment">&lt;!-- jackson --></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.fasterxml.jackson.core<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>jackson-core<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.11.1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">></span></span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>三、功能实现</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>flink<span class="token punctuation">.</span>api<span class="token punctuation">.</span>common<span class="token punctuation">.</span>functions<span class="token punctuation">.</span></span><span class="token class-name">RuntimeContext</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>flink<span class="token punctuation">.</span>streaming<span class="token punctuation">.</span>api<span class="token punctuation">.</span>datastream<span class="token punctuation">.</span></span><span class="token class-name">DataStreamSource</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>flink<span class="token punctuation">.</span>streaming<span class="token punctuation">.</span>api<span class="token punctuation">.</span>environment<span class="token punctuation">.</span></span><span class="token class-name">StreamExecutionEnvironment</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>flink<span class="token punctuation">.</span>streaming<span class="token punctuation">.</span>connectors<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span></span><span class="token class-name">ElasticsearchSinkFunction</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>flink<span class="token punctuation">.</span>streaming<span class="token punctuation">.</span>connectors<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span></span><span class="token class-name">RequestIndexer</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>flink<span class="token punctuation">.</span>streaming<span class="token punctuation">.</span>connectors<span class="token punctuation">.</span>elasticsearch7<span class="token punctuation">.</span></span><span class="token class-name">ElasticsearchSink</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpHost</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>index<span class="token punctuation">.</span></span><span class="token class-name">IndexRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">Requests</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">ArrayList</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">HashMap</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">List</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Map</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FlinkElasticsearchSinkTest</span> <span class="token punctuation">&#123;</span>

	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">&#123;</span>

		<span class="token class-name">StreamExecutionEnvironment</span> env <span class="token operator">=</span> <span class="token class-name">StreamExecutionEnvironment</span><span class="token punctuation">.</span><span class="token function">getExecutionEnvironment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">DataStreamSource</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">></span></span> source <span class="token operator">=</span> env<span class="token punctuation">.</span><span class="token function">socketTextStream</span><span class="token punctuation">(</span><span class="token string">"localhost"</span><span class="token punctuation">,</span> <span class="token number">9999</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">HttpHost</span><span class="token punctuation">></span></span> httpHosts <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		httpHosts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">"127.0.0.1"</span><span class="token punctuation">,</span> <span class="token number">9200</span><span class="token punctuation">,</span> <span class="token string">"http"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token comment">//httpHosts.add(new HttpHost("10.2.3.1", 9200, "http"));</span>

		<span class="token comment">// use a ElasticsearchSink.Builder to create an ElasticsearchSink</span>
		<span class="token class-name">ElasticsearchSink<span class="token punctuation">.</span>Builder</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">></span></span> esSinkBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ElasticsearchSink<span class="token punctuation">.</span>Builder</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span>httpHosts<span class="token punctuation">,</span> 
			<span class="token keyword">new</span> <span class="token class-name">ElasticsearchSinkFunction</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
				<span class="token keyword">public</span> <span class="token class-name">IndexRequest</span> <span class="token function">createIndexRequest</span><span class="token punctuation">(</span><span class="token class-name">String</span> element<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
					<span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">></span></span> json <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
					json<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"data"</span><span class="token punctuation">,</span> element<span class="token punctuation">)</span><span class="token punctuation">;</span>
					<span class="token keyword">return</span> <span class="token class-name">Requests</span><span class="token punctuation">.</span><span class="token function">indexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
						<span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">"my-index"</span><span class="token punctuation">)</span>
						<span class="token comment">//.type("my-type")</span>
						<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>json<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>

				<span class="token annotation punctuation">@Override</span>
				<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">process</span><span class="token punctuation">(</span><span class="token class-name">String</span> element<span class="token punctuation">,</span> <span class="token class-name">RuntimeContext</span> ctx<span class="token punctuation">,</span> <span class="token class-name">RequestIndexer</span> indexer<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
					indexer<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token function">createIndexRequest</span><span class="token punctuation">(</span>element<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">)</span><span class="token punctuation">;</span>
		
		<span class="token comment">// configuration for the bulk requests; this instructs the sink to emit after every element, otherwise they would be buffered</span>
		esSinkBuilder<span class="token punctuation">.</span><span class="token function">setBulkFlushMaxActions</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

		<span class="token comment">// provide a RestClientFactory for custom configuration on the internally createdREST client</span>
		<span class="token comment">// esSinkBuilder.setRestClientFactory(</span>
		<span class="token comment">// restClientBuilder -> &#123;</span>
			<span class="token comment">// restClientBuilder.setDefaultHeaders(...)</span>
			<span class="token comment">// restClientBuilder.setMaxRetryTimeoutMillis(...)</span>
			<span class="token comment">// restClientBuilder.setPathPrefix(...)</span>
			<span class="token comment">// restClientBuilder.setHttpClientConfigCallback(...)</span>
		<span class="token comment">// &#125;</span>
		<span class="token comment">// );</span>
		source<span class="token punctuation">.</span><span class="token function">addSink</span><span class="token punctuation">(</span>esSinkBuilder<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		env<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token string">"flink-es"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h1 id="第6章-Elasticsearch优化"><a href="#第6章-Elasticsearch优化" class="headerlink" title="第6章 Elasticsearch优化"></a>第6章 Elasticsearch优化</h1><h2 id="56-优化-硬件选择"><a href="#56-优化-硬件选择" class="headerlink" title="56-优化-硬件选择"></a>56-优化-硬件选择</h2><p>Elasticsearch的基础是Lucene，所有的索引和文档数据是存储在本地的磁盘中，具体的路径可在ES的配置文件…&#x2F;config&#x2F;elasticsearch.yml中配置，如下：</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token comment">#</span>
<span class="token comment"># Path to directory where to store the data (separate multiple locations by comma):</span>
<span class="token comment">#</span>
<span class="token key atrule">path.data</span><span class="token punctuation">:</span> /path/to/data
<span class="token comment">#</span>
<span class="token comment"># Path to log files:</span>
<span class="token comment">#</span>
<span class="token key atrule">path.logs</span><span class="token punctuation">:</span> /path/to/logs
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>磁盘在现代服务器上通常都是瓶颈。Elasticsearch重度使用磁盘，你的磁盘能处理的吞吐量越大，你的节点就越稳定。这里有一些优化磁盘I&#x2F;O的技巧：</p>
<ul>
<li>使用SSD就像其他地方提过的，他们比机械磁盘优秀多了。</li>
<li>使用RAID0。条带化RAID会提高磁盘IO，代价显然就是当一块硬盘故障时整个就故障了。不要使用镜像或者奇偶校验RAID，因为副本已经提供了这个功能。</li>
<li>另外，使用多块硬盘，并允许Elasticsearch通过多个pathdata目录配置把数据条带化分配到它们上面。</li>
<li>不要使用远程挂载的存储，比如NFS或者SMB&#x2F;CIFS。这个引入的延迟对性能来说完全是背道而驰的。</li>
</ul>
<h2 id="57-优化-分片策略"><a href="#57-优化-分片策略" class="headerlink" title="57-优化-分片策略"></a>57-优化-分片策略</h2><h3 id="合理设置分片数"><a href="#合理设置分片数" class="headerlink" title="合理设置分片数"></a>合理设置分片数</h3><p>分片和副本的设计为ES提供了支持分布式和故障转移的特性，但并不意味着分片和副本是可以无限分配的。而且索引的分片完成分配后由于索引的路由机制，我们是不能重新修改分片数的。</p>
<p>可能有人会说，我不知道这个索引将来会变得多大，并且过后我也不能更改索引的大小，所以为了保险起见，还是给它设为1000个分片吧。但是需要知道的是，一个分片并不是没有代价的。需要了解：</p>
<ul>
<li>一个分片的底层即为一个Lucene索引，会消耗一定文件句柄、内存、以及 CPU运转。</li>
<li>每一个搜索请求都需要命中索引中的每一个分片，如果每一个分片都处于不同的节点还好，但如果多个分片都需要在同一个节点上竞争使用相同的资源就有些糟糕了。</li>
<li>用于计算相关度的词项统计信息是基于分片的。如果有许多分片，每一个都只有很少的数据会导致很低的相关度。</li>
</ul>
<p>一个业务索引具体需要分配多少分片可能需要架构师和技术人员对业务的增长有个预先的判断，横向扩展应当分阶段进行。为下一阶段准备好足够的资源。只有当你进入到下一个阶段，你才有时间思考需要作出哪些改变来达到这个阶段。一般来说，我们遵循一些原则：</p>
<ul>
<li>控制每个分片占用的硬盘容量不超过ES的最大JVM的堆空间设置（一般设置不超过32G，参考下文的JVM设置原则），因此，如果索引的总容量在500G左右，那分片大小在16个左右即可；当然，最好同时考虑原则2。</li>
<li>考虑一下node数量，一般一个节点有时候就是一台物理机，如果分片数过多，大大超过了节点数，很可能会导致一个节点上存在多个分片，一旦该节点故障，即使保持了1个以上的副本，同样有可能会导致数据丢失，集群无法恢复。所以，一般都设置分片数不超过节点数的3倍。</li>
<li>主分片，副本和节点最大数之间数量，我们分配的时候可以参考以下关系：<br><code>节点数&lt;=主分片数*（副本数+1）</code></li>
</ul>
<h3 id="推迟分片分配"><a href="#推迟分片分配" class="headerlink" title="推迟分片分配"></a>推迟分片分配</h3><p>对于节点瞬时中断的问题，默认情况，集群会等待一分钟来查看节点是否会重新加入，如果这个节点在此期间重新加入，重新加入的节点会保持其现有的分片数据，不会触发新的分片分配。这样就可以减少ES在自动再平衡可用分片时所带来的极大开销。</p>
<p>通过修改参数delayed_timeout，可以延长再均衡的时间，可以全局设置也可以在索引级别进行修改：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">#PUT /_all/_settings
<span class="token punctuation">&#123;</span>
	<span class="token property">"settings"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
		<span class="token property">"index.unassigned.node_left.delayed_timeout"</span><span class="token operator">:</span> <span class="token string">"5m"</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="58-优化-路由选择"><a href="#58-优化-路由选择" class="headerlink" title="58-优化-路由选择"></a>58-优化-路由选择</h2><p>当我们查询文档的时候，Elasticsearch如何知道一个文档应该存放到哪个分片中呢？它其实是通过下面这个公式来计算出来：</p>
<pre class="line-numbers language-none"><code class="language-none">shard &#x3D; hash(routing) % number_of_primary_shards
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>routing默认值是文档的id，也可以采用自定义值，比如用户id。</p>
<h3 id="不带routing查询"><a href="#不带routing查询" class="headerlink" title="不带routing查询"></a>不带routing查询</h3><p>在查询的时候因为不知道要查询的数据具体在哪个分片上，所以整个过程分为2个步骤</p>
<ul>
<li>分发：请求到达协调节点后，协调节点将查询请求分发到每个分片上。</li>
<li>聚合：协调节点搜集到每个分片上查询结果，在将查询的结果进行排序，之后给用户返回结果。</li>
</ul>
<h3 id="带routing查询"><a href="#带routing查询" class="headerlink" title="带routing查询"></a>带routing查询</h3><p>查询的时候，可以直接根据routing信息定位到某个分配查询，不需要查询所有的分配，经过协调节点排序。向上面自定义的用户查询，如果routing设置为userid的话，就可以直接查询出数据来，效率提升很多。</p>
<h2 id="59-优化-写入速度优化"><a href="#59-优化-写入速度优化" class="headerlink" title="59-优化-写入速度优化"></a>59-优化-写入速度优化</h2><p>ES 的默认配置，是综合了数据可靠性、写入速度、搜索实时性等因素。实际使用时，我们需要根据公司要求，进行偏向性的优化。</p>
<p>针对于搜索性能要求不高，但是对写入要求较高的场景，我们需要尽可能的选择恰当写优化策略。综合来说，可以考虑以下几个方面来提升写索引的性能：</p>
<ul>
<li>加大Translog Flush，目的是降低Iops、Writeblock。</li>
<li>增加Index Refesh间隔，目的是减少Segment Merge的次数。</li>
<li>调整Bulk线程池和队列。</li>
<li>优化节点间的任务分布。</li>
<li>优化Lucene层的索引建立，目的是降低CPU及IO。</li>
</ul>
<h3 id="优化存储设备"><a href="#优化存储设备" class="headerlink" title="优化存储设备"></a>优化存储设备</h3><p>ES 是一种密集使用磁盘的应用，在段合并的时候会频繁操作磁盘，所以对磁盘要求较高，当磁盘速度提升之后，集群的整体性能会大幅度提高。</p>
<h3 id="合理使用合并"><a href="#合理使用合并" class="headerlink" title="合理使用合并"></a>合理使用合并</h3><p>Lucene以段的形式存储数据。当有新的数据写入索引时，Lucene就会自动创建一个新的段。</p>
<p>随着数据量的变化，段的数量会越来越多，消耗的多文件句柄数及CPU就越多，查询效率就会下降。</p>
<p>由于Lucene段合并的计算量庞大，会消耗大量的I&#x2F;O，所以ES默认采用较保守的策略，让后台定期进行段合并。</p>
<h3 id="减少Refresh的次数"><a href="#减少Refresh的次数" class="headerlink" title="减少Refresh的次数"></a>减少Refresh的次数</h3><p>Lucene在新增数据时，采用了延迟写入的策略，默认情况下索引的refresh_interval为1秒。</p>
<p>Lucene将待写入的数据先写到内存中，超过1秒（默认）时就会触发一次Refresh，然后Refresh会把内存中的的数据刷新到操作系统的文件缓存系统中。</p>
<p>如果我们对搜索的实效性要求不高，可以将Refresh周期延长，例如30秒。</p>
<p>这样还可以有效地减少段刷新次数，但这同时意味着需要消耗更多的Heap内存。</p>
<h3 id="加大Flush设置"><a href="#加大Flush设置" class="headerlink" title="加大Flush设置"></a>加大Flush设置</h3><p>Flush的主要目的是把文件缓存系统中的段持久化到硬盘，当Translog的数据量达到512MB或者30分钟时，会触发一次Flush。</p>
<p>index.translog.flush_threshold_size参数的默认值是512MB，我们进行修改。</p>
<p>增加参数值意味着文件缓存系统中可能需要存储更多的数据，所以我们需要为操作系统的文件缓存系统留下足够的空间。</p>
<h3 id="减少副本的数量"><a href="#减少副本的数量" class="headerlink" title="减少副本的数量"></a>减少副本的数量</h3><p>ES为了保证集群的可用性，提供了Replicas（副本）支持，然而每个副本也会执行分析、索引及可能的合并过程，所以Replicas的数量会严重影响写索引的效率。</p>
<p>当写索引时，需要把写入的数据都同步到副本节点，副本节点越多，写索引的效率就越慢。</p>
<p>如果我们需要大批量进行写入操作，可以先禁止Replica复制，设置<br>index.number_of_replicas:0关闭副本。在写入完成后，Replica修改回正常的状态。</p>
<h2 id="60-优化-内存设置"><a href="#60-优化-内存设置" class="headerlink" title="60-优化-内存设置"></a>60-优化-内存设置</h2><p>ES默认安装后设置的内存是1GB，对于任何一个现实业务来说，这个设置都太小了。如果是通过解压安装的ES，则在ES安装文件中包含一个jvm.option文件，添加如下命令来设置ES的堆大小，Xms表示堆的初始大小，Xmx表示可分配的最大内存，都是1GB。</p>
<p>确保Xmx和Xms的大小是相同的，其目的是为了能够在Java垃圾回收机制清理完堆区后不需要重新分隔计算堆区的大小而浪费资源，可以减轻伸缩堆大小带来的压力。</p>
<p>假设你有一个64G内存的机器，按照正常思维思考，你可能会认为把64G内存都给ES比较好，但现实是这样吗，越大越好？虽然内存对ES来说是非常重要的，但是答案是否定的！</p>
<p>因为ES堆内存的分配需要满足以下两个原则：</p>
<ul>
<li>不要超过物理内存的50%：Lucene的设计目的是把底层OS里的数据缓存到内存中。Lucene的段是分别存储到单个文件中的，这些文件都是不会变化的，所以很利于缓存，同时操作系统也会把这些段文件缓存起来，以便更快的访问。如果我们设置的堆内存过大，Lucene可用的内存将会减少，就会严重影响降低Lucene的全文本查询性能。</li>
<li>堆内存的大小最好不要超过32GB：在Java中，所有对象都分配在堆上，然后有一个KlassPointer指针指向它的类元数据。这个指针在64位的操作系统上为64位，64位的操作系统可以使用更多的内存（2^64）。在32位的系统上为32位，32位的操作系统的最大寻址空间为4GB（2^32）。但是64位的指针意味着更大的浪费，因为你的指针本身大了。浪费内存不算，更糟糕的是，更大的指针在主内存和缓存器（例如LLC,L1等）之间移动数据的时候，会占用更多的带宽。</li>
</ul>
<p>最终我们都会采用31G设置</p>
<ul>
<li>-Xms 31g</li>
<li>-Xmx 31g</li>
</ul>
<p>假设你有个机器有128GB的内存，你可以创建两个节点，每个节点内存分配不超过32GB。也就是说不超过64GB内存给ES的堆内存，剩下的超过64GB的内存给Lucene。</p>
<h2 id="61-优化-重要配置"><a href="#61-优化-重要配置" class="headerlink" title="61-优化-重要配置"></a>61-优化-重要配置</h2><table>
<thead>
<tr>
<th>参数名</th>
<th>参数值</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>cluster.name</td>
<td>elasticsearch</td>
<td>配置ES的集群名称，默认值是ES，建议改成与所存数据相关的名称，ES会自动发现在同一网段下的集群名称相同的节点。</td>
</tr>
<tr>
<td>node.name</td>
<td>node-1</td>
<td>集群中的节点名，在同一个集群中不能重复。节点的名称一旦设置，就不能再改变了。当然，也可以设置成服务器的主机名称，例如node.name:${HOSTNAME}。</td>
</tr>
<tr>
<td>node.master</td>
<td>true</td>
<td>指定该节点是否有资格被选举成为Master节点，默认是True，如果被设置为True，则只是有资格成为Master节点，具体能否成为Master节点，需要通过选举产生。</td>
</tr>
<tr>
<td>node.data</td>
<td>true</td>
<td>指定该节点是否存储索引数据，默认为True。数据 的增、删、改、查都是在Data节点完成的。</td>
</tr>
<tr>
<td>index.number_of_shards</td>
<td>1</td>
<td>设置都索引分片个数，默认是1片。也可以在创建索引时设置该值，具体设置为多大都值要根据数据量的大小来定。如果数据量不大，则设置成1时效率最高</td>
</tr>
<tr>
<td>index.number_of_replicas</td>
<td>1</td>
<td>设置默认的索引副本个数，默认为1个。副本数越多，集群的可用性越好，但是写索引时需要同步的数据越多。</td>
</tr>
<tr>
<td>transport.tcp.compress</td>
<td>true</td>
<td>设置在节点间传输数据时是否压缩，默认为False， 不压缩</td>
</tr>
<tr>
<td>discovery.zen.minimum_master_nodes</td>
<td>1</td>
<td>设置在选举Master节点时需要参与的最少的候选主节点数，默认为1。如果使用默认值，则当网络不稳定时有可能会出现脑裂。合理的数值为(master_eligible_nodes&#x2F;2)+1，其中master_eligible_nodes表示集群中的候选主节点数</td>
</tr>
<tr>
<td>discovery.zen.ping.timeout</td>
<td>3s</td>
<td>设置在集群中自动发现其他节点时Ping连接的超时时间，默认为3秒。在较差的网络环境下需要设置得大一点，防止因误判该节点的存活状态而导致分片的转移</td>
</tr>
</tbody></table>
<h1 id="第7章-Elasticsearch面试题"><a href="#第7章-Elasticsearch面试题" class="headerlink" title="第7章 Elasticsearch面试题"></a>第7章 Elasticsearch面试题</h1><h2 id="62-面试题"><a href="#62-面试题" class="headerlink" title="62-面试题"></a>62-面试题</h2><h3 id="为什么要使用Elasticsearch？"><a href="#为什么要使用Elasticsearch？" class="headerlink" title="为什么要使用Elasticsearch？"></a>为什么要使用Elasticsearch？</h3><p>系统中的数据，随着业务的发展，时间的推移，将会非常多，而业务中往往采用模糊查询进行数据的搜索，而模糊查询会导致查询引擎放弃索引，导致系统查询数据时都是全表扫描，在百万级别的数据库中，查询效率是非常低下的，而我们使用ES做一个全文索引，将经常查询的系统功能的某些字段，比如说电商系统的商品表中商品名，描述、价格还有id这些字段我们放入ES索引库里，可以提高查询速度。</p>
<h3 id="Elasticsearch的master选举流程？"><a href="#Elasticsearch的master选举流程？" class="headerlink" title="Elasticsearch的master选举流程？"></a>Elasticsearch的master选举流程？</h3><ul>
<li>Elasticsearch的选主是ZenDiscovery模块负责的，主要包含Ping（节点之间通过这个RPC来发现彼此）和Unicast（单播模块包含-一个主机列表以控制哪些节点需要ping通）这两部分。</li>
<li>对所有可以成为master的节点（node master:true）根据nodeId字典排序，每次选举每个节点都把自己所知道节点排一次序，然后选出第一个（第0位）节点，暂且认为它是master节点。</li>
<li>如果对某个节点的投票数达到一定的值（可以成为master节点数n&#x2F;2+1）并且该节点自己也选举自己，那这个节点就是master。否则重新选举一直到满足上述条件。</li>
<li>master节点的职责主要包括集群、节点和索引的管理，不负责文档级别的管理；data节点可以关闭http功能。</li>
</ul>
<h3 id="Elasticsearch集群脑裂问题？"><a href="#Elasticsearch集群脑裂问题？" class="headerlink" title="Elasticsearch集群脑裂问题？"></a>Elasticsearch集群脑裂问题？</h3><p>“脑裂”问题可能的成因：</p>
<ul>
<li>网络问题：集群间的网络延迟导致一些节点访问不到master,认为master挂掉了从而选举出新的master,并对master上的分片和副本标红，分配新的主分片。</li>
<li>节点负载：主节点的角色既为master又为data,访问量较大时可能会导致ES停止响应造成大面积延迟，此时其他节点得不到主节点的响应认为主节点挂掉了，会重新选取主节点。</li>
<li>内存回收：data节点上的ES进程占用的内存较大，引发JVM的大规模内存回收，造成ES进程失去响应。</li>
</ul>
<p>脑裂问题解决方案：</p>
<ul>
<li>减少误判：discovery.zen ping_timeout节点状态的响应时间，默认为3s，可以适当调大，如果master在该响应时间的范围内没有做出响应应答，判断该节点已经挂掉了。调大参数（如6s，discovery.zen.ping_timeout:6），可适当减少误判。</li>
<li>选举触发：discovery.zen.minimum._master_nodes:1，该参數是用于控制选举行为发生的最小集群主节点数量。当备选主节点的个數大于等于该参数的值，且备选主节点中有该参数个节点认为主节点挂了，进行选举。官方建议为(n &#x2F;2)+1,n为主节点个数（即有资格成为主节点的节点个数）。</li>
<li>角色分离：即master节点与data节点分离，限制角色<ul>
<li>主节点配置为：node master: true，node data: false</li>
<li>从节点配置为：node master: false，node data: true</li>
</ul>
</li>
</ul>
<h3 id="Elasticsearch索引文档的流程？"><a href="#Elasticsearch索引文档的流程？" class="headerlink" title="Elasticsearch索引文档的流程？"></a>Elasticsearch索引文档的流程？</h3><p><img src="https://img-blog.csdnimg.cn/img_convert/1bdc6c30d1be9b1bff83a683c64d2ac7.png" alt="img"></p>
<ul>
<li>协调节点默认使用文档ID参与计算（也支持通过routing），以便为路由提供合适的分片：shard &#x3D; hash(document_id) % (num_of_primary_shards)</li>
<li>当分片所在的节点接收到来自协调节点的请求后，会将请求写入到MemoryBuffer，然后定时（默认是每隔1秒）写入到FilesystemCache，这个从MemoryBuffer到FilesystemCache的过程就叫做refresh；</li>
<li>当然在某些情况下，存在Momery Buffer和FilesystemCache的数据可能会丢失，ES是通过translog的机制来保证数据的可靠性的。其实现机制是接收到请求后，同时也会写入到translog中，当Filesystemcache中的数据写入到磁盘中时，才会清除掉，这个过程叫做flush；</li>
<li>在flush过程中，内存中的缓冲将被清除，内容被写入一个新段，段的fsync将创建一个新的提交点，并将内容刷新到磁盘，旧的translog将被删除并开始一个新的translog。</li>
<li>flush触发的时机是定时触发（默认30分钟）或者translog变得太大（默认为512M）时；</li>
</ul>
<h3 id="Elasticsearch更新和删除文档的流程？"><a href="#Elasticsearch更新和删除文档的流程？" class="headerlink" title="Elasticsearch更新和删除文档的流程？"></a>Elasticsearch更新和删除文档的流程？</h3><ul>
<li>删除和更新也都是写操作，但是Elasticsearch中的文档是不可变的，因此不能被删除或者改动以展示其变更；</li>
<li>磁盘上的每个段都有一个相应的.del文件。当删除请求发送后，文档并没有真的被删除，而是在.del文件中被标记为删除。该文档依然能匹配查询，但是会在结果中被过滤掉。当段合并时，在.del文件中被标记为删除的文档将不会被写入新段。</li>
<li>在新的文档被创建时，Elasticsearch会为该文档指定一个版本号，当执行更新时，旧版本的文档在.del文件中被标记为删除，新版本的文档被索引到一个新段。旧版本的文档依然能匹配查询，但是会在结果中被过滤掉。</li>
</ul>
<h3 id="Elasticsearch搜索的流程？"><a href="#Elasticsearch搜索的流程？" class="headerlink" title="Elasticsearch搜索的流程？"></a>Elasticsearch搜索的流程？</h3><p><img src="https://img-blog.csdnimg.cn/img_convert/053a14eee04ace7b4e5aec0ce53a5284.png" alt="img"></p>
<ul>
<li>搜索被执行成一个两阶段过程，我们称之为Query Then Fetch；</li>
<li>在初始查询阶段时，查询会广播到索引中每一个分片拷贝（主分片或者副本分片）。每个分片在本地执行搜索并构建一个匹配文档的大小为from+size的优先队列。PS：在搜索的时候是会查询Filesystem Cache的，但是有部分数据还在Memory Buffer，所以搜索是近实时的。</li>
<li>每个分片返回各自优先队列中所有文档的ID和排序值给协调节点，它合并这些值到自己的优先队列中来产生一个全局排序后的结果列表。</li>
<li>接下来就是取回阶段，协调节点辨别出哪些文档需要被取回并向相关的分片提交多个GET请求。每个分片加载并丰富文档，如果有需要的话，接着返回文档给协调节点。一旦所有的文档都被取回了，协调节点返回结果给客户端。</li>
<li>Query Then Fetch的搜索类型在文档相关性打分的时候参考的是本分片的数据，这样在文档数量较少的时候可能不够准确，DFS Query Then Fetch增加了一个预查询的处理，询问Term和Document frequency，这个评分更准确，但是性能会变差。</li>
</ul>
<h3 id="Elasticsearch在部署时，对Linux的设置有哪些优化方法？"><a href="#Elasticsearch在部署时，对Linux的设置有哪些优化方法？" class="headerlink" title="Elasticsearch在部署时，对Linux的设置有哪些优化方法？"></a>Elasticsearch在部署时，对Linux的设置有哪些优化方法？</h3><ul>
<li>64GB内存的机器是非常理想的，但是32GB和16GB机器也是很常见的。少于8GB会适得其反。</li>
<li>如果你要在更快的CPUs和更多的核心之间选择，选择更多的核心更好。多个内核提供的额外并发远胜过稍微快一点点的时钟频率。</li>
<li>如果你负担得起SSD，它将远远超出任何旋转介质。基于SSD的节点，查询和索引性能都有提升。如果你负担得起，SSD是一个好的选择。</li>
<li>即使数据中心们近在咫尺，也要避免集群跨越多个数据中心。绝对要避免集群跨越大的地理距离。</li>
<li>请确保运行你应用程序的JVM和服务器的JVM是完全一样的。在Elasticsearch的几个地方，使用Java的本地序列化。</li>
<li>通过设置gateway.recover_after_nodes、gateway.expected_nodes、gateway.recover_after_time可以在集群重启的时候避免过多的分片交换，这可能会让数据恢复从数个小时缩短为几秒钟。</li>
<li>Elasticsearch默认被配置为使用单播发现，以防止节点无意中加入集群。只有在同一台机器上运行的节点才会自动组成集群。最好使用单播代替组播。</li>
<li>不要随意修改垃圾回收器（CMS）和各个线程池的大小。</li>
<li>把你的内存的（少于）一半给Lucene（但不要超过32GB！），通过ES_HEAP_SIZE环境变量设置。</li>
<li>内存交换到磁盘对服务器性能来说是致命的。如果内存交换到磁盘上，一个100微秒的操作可能变成10毫秒。再想想那么多10微秒的操作时延累加起来。不难看出swapping对于性能是多么可怕。</li>
<li>Lucene使用了大量的文件。同时，Elasticsearch在节点和HTTP客户端之间进行通信也使用了大量的套接字。所有这一切都需要足够的文件描述符。你应该增加你的文件描述符，设置一个很大的值，如64,000。</li>
</ul>
<h3 id="GC方面，在使用Elasticsearch时要注意什么？"><a href="#GC方面，在使用Elasticsearch时要注意什么？" class="headerlink" title="GC方面，在使用Elasticsearch时要注意什么？"></a>GC方面，在使用Elasticsearch时要注意什么？</h3><p>倒排词典的索引需要常驻内存，无法GC，需要监控data node 上 segment memory增长趋势。</p>
<p>各类缓存，field cache, filter cache, indexing cache, bulk queue等等，要设置合理的大小，并且要应该根据最坏的情况来看heap是否够用，也就是各类缓存全部占满的时候，还有heap空间可以分配给其他任务吗？避免采用clear cache等“自欺欺人”的方式来释放内存。</p>
<p>避免返回大量结果集的搜索与聚合。确实需要大量拉取数据的场景，可以采用scan &amp; scroll api来实现。</p>
<p>cluster stats驻留内存并无法水平扩展，超大规模集群可以考虑分拆成多个集群通过tribe node连接。</p>
<p>想知道heap够不够，必须结合实际应用场景，并对集群的heap使用情况做持续的监控。</p>
<h3 id="Elasticsearch对于大数据量（上亿量级）的聚合如何实现？"><a href="#Elasticsearch对于大数据量（上亿量级）的聚合如何实现？" class="headerlink" title="Elasticsearch对于大数据量（上亿量级）的聚合如何实现？"></a>Elasticsearch对于大数据量（上亿量级）的聚合如何实现？</h3><p>Elasticsearch提供的首个近似聚合是cardinality度量。它提供一个字段的基数，即该字段的 distinct或者unique值的数目。它是基于HLL算法的。HLL会先对我们的输入作哈希运算，然后根据哈希运算的结果中的bits做概率估算从而得到基数。其特点是：可配置的精度，用来控制内存的使用（更精确＝更多内存）；小的数据集精度是非常高的；我们可以通过配置参数，来设置去重需要的固定内存使用量。无论数千还是数十亿的唯一值，内存使用量只与你配置的精确度相关。</p>
<h3 id="在并发情况下，Elasticsearch如果保证读写一致？"><a href="#在并发情况下，Elasticsearch如果保证读写一致？" class="headerlink" title="在并发情况下，Elasticsearch如果保证读写一致？"></a>在并发情况下，Elasticsearch如果保证读写一致？</h3><ul>
<li>可以通过版本号使用乐观并发控制，以确保新版本不会被旧版本覆盖，由应用层来处理具体的冲突；</li>
<li>另外对于写操作，一致性级别支持quorum&#x2F;one&#x2F;all，默认为quorum，即只有当大多数分片可用时才允许写操作。但即使大多数可用，也可能存在因为网络等原因导致写入副本失败，这样该副本被认为故障，分片将会在一个不同的节点上重建。</li>
<li>对于读操作，可以设置replication为sync(默认)，这使得操作在主分片和副本分片都完成后才会返回；如果设置replication为async时，也可以通过设置搜索请求参数_preference为primary来查询主分片，确保文档是最新版本。</li>
</ul>
<h3 id="如何监控Elasticsearch集群状态？"><a href="#如何监控Elasticsearch集群状态？" class="headerlink" title="如何监控Elasticsearch集群状态？"></a>如何监控Elasticsearch集群状态？</h3><ol>
<li>elasticsearch-head插件。</li>
<li>通过Kibana监控Elasticsearch。你可以实时查看你的集群健康状态和性能，也可以分析过去的集群、索引和节点指标</li>
</ol>
<h3 id="是否了解字典树？"><a href="#是否了解字典树？" class="headerlink" title="是否了解字典树？"></a>是否了解字典树？</h3><p>字典树又称单词查找树，Trie树，是一种树形结构，是一种哈希树的变种。典型应用是用于统计，排序和保存大量的字符串（但不仅限于字符串），所以经常被搜索引擎系统用于文本词频统计。它的优点是：利用字符串的公共前缀来减少查询时间，最大限度地减少无谓的字符串比较，查询效率比哈希树高。</p>
<p>Trie的核心思想是空间换时间，利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的。它有3个基本性质：</p>
<ul>
<li>根节点不包含字符，除根节点外每一个节点都只包含一个字符。</li>
<li>从根节点到某一节点，路径上经过的字符连接起来，为该节点对应的字符串。</li>
<li>每个节点的所有子节点包含的字符都不相同。</li>
</ul>
<p>对于中文的字典树，每个节点的子节点用一个哈希表存储，这样就不用浪费太大的空间，而且查询速度上可以保留哈希的复杂度O(1)。</p>
<h3 id="Elasticsearch中的集群、节点、索引、文档、类型是什么？"><a href="#Elasticsearch中的集群、节点、索引、文档、类型是什么？" class="headerlink" title="Elasticsearch中的集群、节点、索引、文档、类型是什么？"></a>Elasticsearch中的集群、节点、索引、文档、类型是什么？</h3><ul>
<li>集群是一个或多个节点（服务器）的集合，它们共同保存您的整个数据，并提供跨所有节点的联合索引和搜索功能。群集由唯一名称标识，默认情况下为”elasticsearch”。此名称很重要，因为如果节点设置为按名称加入群集，则该节点只能是群集的一部分。</li>
<li>节点是属于集群一部分的单个服务器。它存储数据并参与群集索引和搜索功能。</li>
<li>索引就像关系数据库中的“数据库”。它有一个定义多种类型的映射。索引是逻辑名称空间，映射到一个或多个主分片，并且可以有零个或多个副本分片。MySQL&#x3D;&gt;数据库，Elasticsearch&#x3D;&gt;索引。</li>
<li>文档类似于关系数据库中的一行。不同之处在于索引中的每个文档可以具有不同的结构(字段)，但是对于通用字段应该具有相同的数据类型。MySQL&#x3D;&gt;Databases&#x3D;&gt;Tables&#x3D;&gt;Columns&#x2F;Rows，Elasticsearch&#x3D;&gt;Indices&#x3D;&gt;Types&#x3D;&gt;具有属性的文档Doc。</li>
<li>类型是索引的逻辑类别&#x2F;分区，其语义完全取决于用户。</li>
</ul>
<h3 id="Elasticsearch中的倒排索引是什么？"><a href="#Elasticsearch中的倒排索引是什么？" class="headerlink" title="Elasticsearch中的倒排索引是什么？"></a>Elasticsearch中的倒排索引是什么？</h3><p>倒排索引是搜索引擎的核心。搜索引擎的主要目标是在查找发生搜索条件的文档时提供快速搜索。ES中的倒排索引其实就是lucene的倒排索引，区别于传统的正向索引，倒排索引会再存储数据时将关键词和数据进行关联，保存到倒排表中，然后查询时，将查询内容进行分词后在倒排表中进行查询，最后匹配数据即可。</p>
<h1 id="相关文章"><a href="#相关文章" class="headerlink" title="相关文章"></a>相关文章</h1><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/UjVH0IhSrbyk9MTKi7Nwhg">ElasticSearch自定义中文分词插件开发介绍</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=MzkzMDY1NDgyOQ==&mid=2247780376&idx=3&sn=e297e34815947062fb4da86301561238&source=41#wechat_redirect">一篇就懂Elasticsearch</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/7OmlaV7yrod2_5FWyq-Y4Q">Elasticsearch为什么能做到快速检索？秘密在这里！</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/WavG7tl-xZDn7hfw24vDuw">手把手教你使用Elasticsearch实现海量级数据搜索（上）</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/LX2cKF6wH1IvlxmCSnWgCw">手把手教你在CentOS上安装ELK，对服务器日志进行收集</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ZBsv3o5XfQq7gZkqa2yA6Q">2万字详解，彻底讲透全文搜索引擎Elasticsearch</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Quoym4438irm4Uexb40asw">ElasticSearch深度分页详解</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/nOVXS4XjNwig_2wNhaCYVw">来聊一聊ElasticSearch最新版的Java客户端</a></td>
<td align="center"></td>
</tr>
</tbody></table>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/blog/about" rel="external nofollow noreferrer">xmxe</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://xmxe.github.io/blog/posts/38f9e76ec464/">https://xmxe.github.io/blog/posts/38f9e76ec464/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/blog/about" target="_blank">xmxe</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            <span class="chip bg-color">无标签</span>
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/blog/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="qq,qzone,wechat,weibo,douban" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/blog/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        
                            <img src="/blog/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                        
                    </div>
                    <div id="wechat">
                        
                            <img src="/blog/medias/reward/wechat.jpg" class="reward-img" alt="微信打赏二维码">
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/blog/posts/8c045b869342/">
                    <div class="card-image">
                        
                        <img src="https://picd.zhimg.com/v2-06d026cb3aaade2aa5380ee31d099a85_1440w.jpg" class="responsive-img" alt="Oauth2">
                        
                        <span class="card-title">Oauth2</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
demofork from oauth2-samples




Demo
文章



authorization_code
这个案例写出来，还怕跟面试官扯不明白OAuth2登录流程？


client_credentials
死磕OAu
                        

                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <!--
                            <i class="far fa-clock fa-fw icon-date"></i>2022-11-15
                            -->

                            
                                <i class="fas fa-user fa-fw"></i>
                                <a href="/blog/about" >
                                    xmxe
                                </a>
                            
                        </span>
                        <span class="publish-author">
                            
                                <i class="fas fa-bookmark fa-fw icon-category"></i>
                                
                                    <a href="/blog/categories/%E6%8A%80%E6%9C%AF%E6%A0%88/" class="post-category">
                                        技术栈
                                    </a>
                                
                            

                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/blog/posts/566ad91076fb/">
                    <div class="card-image">
                        
                        <img src="https://picd.zhimg.com/v2-5f6b4a9d35a1add123ac6b3f444b2291_1440w.jpg" class="responsive-img" alt="Dubbo">
                        
                        <span class="card-title">Dubbo</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            Dubbo基础什么是Dubbo?
Apache Dubbo|ˈdʌbəʊ|是一款高性能、轻量级的开源Java RPC框架。根据 Dubbo官方文档的介绍，Dubbo提供了六大核心能力

面向接口代理的高性能RPC调用。
智能容错和负载均衡。
                        
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <!--
                            <i class="far fa-clock fa-fw icon-date"></i>2022-11-15
                            -->
                            
                                <i class="fas fa-user fa-fw"></i>
                                <a href="/blog/about" >
                                    xmxe
                                </a>
                            
                        </span>
                        <span class="publish-author">
                            
                                <i class="fas fa-bookmark fa-fw icon-category"></i>
                                
                                    <a href="/blog/categories/%E6%8A%80%E6%9C%AF%E6%A0%88/" class="post-category">
                                        技术栈
                                    </a>
                                
                            
                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/blog/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/blog/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/blog/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="song"
                   id="569200213"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/blog/libs/aplayer/APlayer.min.js"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script> -->

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
            
                <span id="year">2022-2025
                </span>
            
            

            <a href="/blog/about" target="_blank">
                xmxe
            </a>
            |&nbsp;Powered by&nbsp;
            <a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;
            <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>

            
            
            
            
            
            

            
            <br>

            
            <br>

            
        </div>
        <div class="col s12 m4 l4 social-link2 ">
    <a href="https://github.com/xmxe" class="tooltipped" target="_blank" data-tooltip="GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="https://gitee.com/xmxe" class="tooltipped" target="_blank" data-tooltip="码云" data-position="top" data-delay="50">
        <svg width="19" height="19" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" style="position: relative; top: 2px; left: 0.5px;">
            <path d="M512 1024C230.4 1024 0 793.6 0 512S230.4 0 512 0s512 230.4 512 512-230.4 512-512 512z m259.2-569.6H480c-12.8 0-25.6 12.8-25.6 25.6v64c0 12.8 12.8 25.6 25.6 25.6h176c12.8 0 25.6 12.8 25.6 25.6v12.8c0 41.6-35.2 76.8-76.8 76.8h-240c-12.8 0-25.6-12.8-25.6-25.6V416c0-41.6 35.2-76.8 76.8-76.8h355.2c12.8 0 25.6-12.8 25.6-25.6v-64c0-12.8-12.8-25.6-25.6-25.6H416c-105.6 0-188.8 86.4-188.8 188.8V768c0 12.8 12.8 25.6 25.6 25.6h374.4c92.8 0 169.6-76.8 169.6-169.6v-144c0-12.8-12.8-25.6-25.6-25.6z" fill="#fff">
            </path>
        </svg>
    </a>

















    
        
          <a href="/blog/download" class="tooltipped" target="_blank" data-tooltip="下载" data-position="top" data-delay="50">
            <i class="fas fa-download"></i>
          </a>
        
    



    <style>
  .mobiledevice {
    display: none !important;
  }

  footer .wechat_qrcode {
    position: fixed;
  }

  /*微信二维码*/
  .wechat_qrcode {
    position: absolute;
    margin-left: 10px;
    bottom: 10px;
    background: url("/blog/medias/xcx.png");
    zoom:40%;
  }

  .wechat:hover .wechat_qrcode {
    width: 430px;
    height: 430px;
    animation: move 0.4s linear 1 normal;
  }

  @keyframes move {
    0% {
      transform: translate(100px, 0);
      opacity: 0;
    }
    50% {
      transform: translate(50px, 0);
      opacity: 0.5;
    }
    100% {
      transform: translate(0, 0);
      opacity: 1;
    }
  }

  @media only screen and (max-width: 601px) {
    .wechat {
      display: none !important;
    }
    .mobiledevice {
      display: inline-block !important;
    }
  }
</style>

<a href="javascript:;" class="wechat" data-position="top" data-delay="50">
  <i class="fab fa-weixin"></i>
  <img class="wechat_qrcode" />
</a>

<a
  href="javascript:;"
  class="tooltipped mobiledevice"
  data-tooltip="微信: 464817304"
  data-position="top"
  data-delay="50"
>
  <i class="fab fa-weixin"></i>
</a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/blog/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>

    <div class="stars-con">
  <div id="stars"></div>
  <div id="stars2"></div>
  <div id="stars3"></div>  
</div>

<!-- 白天和黑夜主题 -->



<script>
  function switchNightMode() {
    
      setTimeout(function () {
        $('body').hasClass('DarkMode') 
        ? ($('body').removeClass('DarkMode'), localStorage.setItem('isDark', '0'), $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')) 
        : ($('body').addClass('DarkMode'), localStorage.setItem('isDark', '1'), $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')),
          
        setTimeout(function () {
          $('.Cuteen_DarkSky').fadeOut(1e3, function () {
            $(this).remove()
          })
        }, 2e3)
      })
  }
</script>
    
    
    <script>
        /* 模式判断 */
        if (localStorage.getItem('isDark') === '1') {
            document.body.classList.add('DarkMode');
            $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')
        } else {
            document.body.classList.remove('DarkMode');
            $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')
        }
    </script>

    <script src="/blog/libs/materialize/materialize.min.js"></script>
    <script src="/blog/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/blog/libs/aos/aos.js"></script>
    <script src="/blog/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/blog/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/blog/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    

    

    

	
    

    

    
    <script type="text/javascript" src="/blog/libs/background/ribbon-dynamic.js" async="async"></script>
    

    

    <!-- 冒泡 -->
    
    <script type="text/javascript">
        // 只在桌面版网页启用特效
        // var windowWidth = $(window).width();
        
            document.write('<script type="text/javascript" src="/blog/libs/others/bubleAll.js"><\/script>');
        
        
    </script>
    

    <!-- 弹出文字 -->
    

</body>

</html>
